Comparing a string with another


(Eliseo Dannunzio) #1

Squiz Matrix currently has ways of comparing numbers, with boolean keyword modifiers for equating or determining which number is greater or less than another number. I noticed we don't have a modifier to equate strings...

 

After some experimentation, I developed this little coding nugget of goodness...

%variable_to_be_compared^explode:~^in_array:string_to_be_compared:string_if_found:string_if_not_found%

An example of this would be:

%name_of_client^explode:~^in_array:John:Hello John:Hello stranger%

Naturally, the use of `~` is arbitrary, as this character is meant to be one guaranteed not to be in the variable's value. choosing something obscure like the pound sterling symbol would make things even easier, but exploding a string into an array using a delimiter character not found in the string forces the array into a single element array that can be searched in, and will return TRUE for a matching string, and FALSE for a non-matching string. As it's unlikely that a tilde would appear in a generic string value, I've used that within my example.

 

I hope this helps anyone looking for a quick and dirty way of matching strings...

 

~ Eliseo


(Peter McLeod) #2

Hi

 

I'm using v5.3.1.1 and and the 'eq' modifier seems to work ok as a case sensitive string comparision.

 

Not sure if this has always been the case, the docs still refer to it as working on numerical values.

 

But in some of the more recent examples on the manuals site such as the conditions pagem, there are examples of string comparision -eg:

 

%begin_asset_metadata_foo^:eq:foo:notempty:%

 

https://matrix-manuals.squiz.net/keyword-replacements/chapters/conditional-keywords

 

Thanks

Peter


(Anthony Ponomarenko) #3

There is also the 'contains' modifier:

 

%asset_name^contains:Foo:Found:Not Found%