Page 1 of 1

if variable matches to a variable with multiple values

Posted: 15 Sep 2021 23:14
by hermhart
Is there a way, that if I get a list of values like so:

Code: Select all

 $overwrite = get("selecteditemsnames", "<crlf>");
And it returns the values for $overwrite as:
1.txt
2.txt
3.txt

That I can compare a variable with a single value to the list of values in $overwrite, and if it matches any of the values in $overwrite, than it performs the action, perhaps like so (though I cannot get it to work)?

Code: Select all

 $file = 2.txt;
 if (regexmatches($file, $overwrite, <crlf>)) {
    $contents = Action: OVERWRITE;
 }

Re: if variable matches to a variable with multiple values

Posted: 15 Sep 2021 23:25
by highend
Sure

but

1. Trying to match a list with a single item is wrong, it's the other way round
2. Your regexmatches() would not work anyway because the single item needs to be escaped before

Code: Select all

function IsIn($listOfVariables, $variable) {
    $esc = regexreplace($variable, "([\\.+(){\[^$])", "\$1" . "$");
    if (regexmatches($listOfVariables, $esc)) { return true; }
    return false;
}
or using gettokenindex() could be used for this (but only for full matches^^) as well...

Re: if variable matches to a variable with multiple values

Posted: 15 Sep 2021 23:31
by hermhart
Thanks!