Search Replace Array

Q and A about functionality and how to solve a special task for your application.

Moderator: Support

Post Reply
User avatar
Naggar
Posts: 117
Joined: 22 Dec 2016, 14:02
Location: Dubai, United Arab Emirates

Search Replace Array

Post by Naggar » 11 Jul 2018, 12:42

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?

User avatar
chriss0212
Posts: 648
Joined: 18 Jan 2012, 20:56
Location: wuppertal
Contact:

Re: Search Replace Array

Post by chriss0212 » 20 Jul 2018, 10:43

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...

User avatar
lerou
Posts: 304
Joined: 06 Sep 2013, 07:14
Location: Hamburg, Germany

Re: Search Replace Array

Post by lerou » 20 Jul 2018, 11:30

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;
}
Attachments
replace_chars_001.vza
(6.88 KiB) Downloaded 37 times

User avatar
chriss0212
Posts: 648
Joined: 18 Jan 2012, 20:56
Location: wuppertal
Contact:

Re: Search Replace Array

Post by chriss0212 » 20 Jul 2018, 15:10

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

User avatar
lerou
Posts: 304
Joined: 06 Sep 2013, 07:14
Location: Hamburg, Germany

Re: Search Replace Array

Post by lerou » 20 Jul 2018, 15:12

you're right. Sort the arrays first then it would work.

User avatar
chriss0212
Posts: 648
Joined: 18 Jan 2012, 20:56
Location: wuppertal
Contact:

Re: Search Replace Array

Post by chriss0212 » 20 Jul 2018, 15:13

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 ;)

User avatar
lerou
Posts: 304
Joined: 06 Sep 2013, 07:14
Location: Hamburg, Germany

Re: Search Replace Array

Post by lerou » 20 Jul 2018, 15:37

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;
Attachments
replace_chars_002.vza
(9.01 KiB) Downloaded 35 times

zding
Posts: 40
Joined: 02 Mar 2018, 20:43

Re: Search Replace Array

Post by zding » 24 Jul 2018, 04:55

Queue?In the future A control should be added

User avatar
Naggar
Posts: 117
Joined: 22 Dec 2016, 14:02
Location: Dubai, United Arab Emirates

Re: Search Replace Array

Post by Naggar » 29 Aug 2018, 13:29

This is great guys
Leru, exactly what I needed :D Thanks a lot!

joschy
Posts: 135
Joined: 19 Jan 2012, 07:39
Location: Düsseldorf, Germany
Contact:

Re: Search Replace Array

Post by joschy » 07 Sep 2018, 11:22

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.
ConvertToLetters.vzs
(17.89 KiB) Downloaded 33 times
The input is a property at the container - ConvertToLetters
3DJo - [Ventuz] freelancer and trainer

Post Reply