How best to check if string contains any of a list of substrings?
Keith Clarke
keith.clarke at me.com
Thu Nov 1 07:02:29 EDT 2018
Thanks Alex - I thought the single-pass ‘is among’ with wildcards was a long-shot, so I’ll concentrate on debugging the list iteration using the ‘contains’ check. :-)
The strings & sub-strings are simple - I have a large list of URLs that need to be processed differently (or ignored), based on various sub-string ’signatures’ that route them into different ‘buckets’ in an array.
The ’signatures’ vary - hence my use of a list variable (derived from a field) rather than hard-coding the plain text as literals.
BTW Good spot on the asterisk wildcard characters in the contains example below. This is just a typo in the email - they’re not (among the numerous bugs!) in the real code. :-)
Thanks again for the steer.
Best,
Keith
> On 1 Nov 2018, at 10:34, Alex Tweedly via use-livecode <use-livecode at lists.runrev.com> wrote:
>
> Hi Keith,
>
> I think the simplest method would be (almost) your second attempt below. However, you were using "*est*" - i.e. with the '*'s before and after the phrase, which is what you would need for 'filter' but not for 'contains'. So it should work with just
>
>> put “my test phrase” into myTestPhrase
>> put “est” & cr & “phr” into tSubstrings
>>
>> repeat for each line L in tSubstrings
>> if myTestPhrase contains L then answer “true”
>> end repeat
> There's almost certainly also a way of doing it in a single pass using regex - but I'm not brave enough to go there :-)
>
> Are you expecting reallylarge strings to search ? Or really many phrases to search for ?
>
> "beware optimizing something that is just plain fast enough already"
>
> Alex.
>
> On 01/11/2018 09:41, Keith Clarke via use-livecode wrote:
>> Folks,
>> What is the most efficient way to test whether a variable containing a string such as “my test phrase” contains one of several variable substrings, such as “est” OR “phr” OR... ?
>>
>> So far I’ve tried (without luck) approaches that simplify into...
>>
>> put “my test phrase” into myTestPhrase
>> put “*est*” & cr & “*phr*” into tSubstrings
>>
>> if myTestPhrase is among the lines of tSubstrings then answer “true”
>>
>> repeat for each line L in tSubstrings
>> if myTestPhrase contains L then answer “true”
>> end repeat
>>
>> Both seem to be failing - do I have the wrong syntax or approach to the problem?
>>
>> TIA
>> Keith
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
More information about the use-livecode
mailing list