Page 1 of 1
Search Replace Array
Posted: 11 Jul 2018, 12:42
by Naggar
Hey guys..
Was wondering if there is a way to use Search and replace node inputting Search Array and Replace Array?
For example:
Input: 2018 - 10 - 13
Search: 0,1,2,3,4,5,6,7,8,9,10,11,12,13
Replace: a,b,c,d,e,f,g,h,i,j,k,l,m,n
Output = cabi - k - n
or Something similar to that?
Re: Search Replace Array
Posted: 20 Jul 2018, 10:43
by chriss0212
If so, than just by a script...
But i think, that even with a script it is maybe complex, because 2018 should be replace letter by letter but 10 should be replaced in total...
Re: Search Replace Array
Posted: 20 Jul 2018, 11:30
by lerou
you can do this in a string expression. See attached vza. Of course if the string is very large or the number of replacements is very high, at some point this might have an impact on the performance.
Code: Select all
{
string res = A;
if( !(string.IsNullOrWhiteSpace(B) || string.IsNullOrWhiteSpace(C)) )
{
var a = B.Split(',');
var b = C.Split(',');
int max = Math.Max(a.Length, b.Length);
for(int i = 0; i < max; ++i)
{
res = res.Replace(a[i], b[i]);
}
}
return res;
}
Re: Search Replace Array
Posted: 20 Jul 2018, 15:10
by chriss0212
Hi rou,
nice example but... as i thought, it doesn't work the way he likes
2018-10-13 should be cabi-k-n but in your example it is cabi-ba-bd
For eg. the 10 is replaced by ba... what is correct for me because first you check for 0 what has a replacement... then for 1 what have a replacement and than, there is no more 10 existing

... but he like to replace it by k!
So you need to make sure, that the search AND replace string is in the right order.
If you change it to:
Search: 10,11,12,13,0,1,2,3,4,5,6,7,8,9
Replace: k,l,m,n,a,b,c,d,e,f,g,h,i,j
It could work
Greetings
Christian
Re: Search Replace Array
Posted: 20 Jul 2018, 15:12
by lerou
you're right. Sort the arrays first then it would work.
Re: Search Replace Array
Posted: 20 Jul 2018, 15:13
by chriss0212
Hi Rou...
By the way: didn't knew, that it works with a single Expression
Thank you!!
And i have tested the other order in the strings... works

Re: Search Replace Array
Posted: 20 Jul 2018, 15:37
by lerou
okok. Here we go again:
Code: Select all
string res = inText;
if(!( string.IsNullOrWhiteSpace(inText) || string.IsNullOrWhiteSpace(inSearch) || string.IsNullOrWhiteSpace(inReplace) ) )
{
var a = inSearch.Split(',');
var b = inReplace.Split(',');
if( a.Length == b.Length )
{
var pairs = new List<Tuple<string,string>>();
for(int i = 0; i < a.Length; ++i)
{
pairs.Add( new Tuple<string,string>(a[i], b[i]));
}
pairs.Sort((left, right) => -left.Item1.Length.CompareTo(right.Item1.Length) );
foreach(var pair in pairs)
{
res = res.Replace(pair.Item1, pair.Item2);
}
}
}
outText = res;
Re: Search Replace Array
Posted: 24 Jul 2018, 04:55
by zding
Queue?In the future A control should be added
Re: Search Replace Array
Posted: 29 Aug 2018, 13:29
by Naggar
This is great guys
Leru, exactly what I needed

Thanks a lot!
Re: Search Replace Array
Posted: 07 Sep 2018, 11:22
by joschy
Nice solution.
Here is a solution with simple ventuz nodes without scripting.
Because you can handle the numbers as a indexer for the letters array you not really need a replacement, that`s my thought.
The input is a date YYYY - MM - DD format, i only have to split the year number in its parts. The month and day numbers can be used as they are.
The input is a property at the container - ConvertToLetters