Page 1 of 1

If (Var == "value1|value2|value3")

Posted: 20 Sep 2013 12:41
by binocular222
I remembered this works before:
If("<curext>" == "docx|doc|xlsx|xls") {msg ok}

But it doesn't now. Anything wrong?
Besides, where is this kind of comparision documented in help file?

Re: If (Var == "value1|value2|value3")

Posted: 20 Sep 2013 12:44
by Marco
Shouldn't it be

Code: Select all

If("<curext>" == "docx" | "<curext>" == "doc" | "<curext>" == "xlsx" | "<curext>" == "xls") {msg ok}

Re: If (Var == "value1|value2|value3")

Posted: 20 Sep 2013 12:45
by highend
Anything wrong?
E.g.:

Code: Select all

If("docx|doc|xlsx|xls" LikeI "*<curext>*") {msg ok}

Code: Select all

where is this kind of comparision documented in help file?
Advanced Topics - Scripting - Comparisons

Re: If (Var == "value1|value2|value3")

Posted: 20 Sep 2013 13:32
by binocular222
Thanks, I know both these methods but think that If(Var == "value1|value2|value3") would be better. I did saw this kind of expression in a xys script somethime ago aghhh.
If("docx|doc|xlsx|xls" LikeI "*ocx*") would result in True eventhough I want "docx".

If("<curext>" == "docx" | "<curext>" == "doc" | "<curext>" == "xlsx" | "<curext>" == "xls") is accurate, but too lenghthy

Re: If (Var == "value1|value2|value3")

Posted: 20 Sep 2013 14:08
by TheQwerty
binocular222 wrote:I did saw this kind of expression in a xys script somethime ago aghhh.
If("docx|doc|xlsx|xls" LikeI "*ocx*") would result in True eventhough I want "docx".
Which can be corrected by surrounding the needle and haystack with the separator like so:

Code: Select all

if ("|docx|doc|xlsx|xls|" LikeI "*|ocx|*")
That said I think a FindToken function would be a great alternative and also extremely useful.

Maybe something like:

Code: Select all

FindToken(token, list, [separator=" "], [flags=iw]);

Flags:
  i = Case Insensitive
  r = Return index from end.
  w = Use wildcards (If missing then token is treated literally. If included and token does not contain wildcards treat it as *token*.)

Returns:
 0 = If token was not found in the list.
 Otherwise return the index of the token within the list.

Re: If (Var == "value1|value2|value3")

Posted: 20 Sep 2013 14:42
by FluxTorpedoe
TheQwerty wrote:FindToken(token, list, [separator=" "], [flags=iw]);
Always eager when someone's talking about a new toy! :lol:
In this case, what would be the difference with strpos()?

BTW, especially with Like, I'm a partisan of the "surrounding the needle and haystack with the separator" too. It doesn't always feel "clean", but it feels "safe"!

PS: I don't add anything to the OP since the collective answers already sum it quite well... :)

Re: If (Var == "value1|value2|value3")

Posted: 20 Sep 2013 15:52
by TheQwerty
FluxTorpedoe wrote:
TheQwerty wrote:FindToken(token, list, [separator=" "], [flags=iw]);
Always eager when someone's talking about a new toy! :lol:
In this case, what would be the difference with strpos()?
StrPos() returns the first matching position within a string and is zero-based.
FindToken() returns the first matching token index within the list and is one-based.

Code: Select all

$haystack = 'a b c d';
$needle = 'c';

$strPos = StrPos($haystack, $needle); // == 4. It is the fourth character.
$tokPos = FindToken($needle, $haystack); // == 3. It is the third token.
Note that comparing its syntax to StrPos makes it seem extremely inconsistent, so instead compare it to GetToken. ;)

And yes this is just a function to replace a rather simple while/foreach loop, but being a function allows it to be better used in conditionals. Alternately the ability to create users functions would obviate the need for this function.

Re: If (Var == "value1|value2|value3")

Posted: 20 Sep 2013 17:04
by admin
I somehow don't get the use of FindToken. When you look for a token in a string, you already know that token (else you cannot look for it). So what use is it to get the position of that token? Isn't it enough to know that the token is somewhere in the string? (for which strpos() is enough)

Re: If (Var == "value1|value2|value3")

Posted: 20 Sep 2013 17:55
by TheQwerty
admin wrote:I somehow don't get the use of FindToken. When you look for a token in a string, you already know that token (else you cannot look for it). So what use is it to get the position of that token? Isn't it enough to know that the token is somewhere in the string? (for which strpos() is enough)
Because knowing the position of the token makes it easier to then retrieve/act on the previous or next token - something that strpos is not especially helpful with.

Say I'm using a set of tags to mark the state of a file: "idea|designed|developed|complete".
And I want a script to move all selected items to the next state.

Strpos isn't well suited for this.
Instead you'd likely write a separate script that accepts a list and a token.
It loops through the list to find the token and either "returns" the index or the next token.


Similarly as this thread shows, using only StrPos to locate a token within a list means that you have to give some consideration to whether your needle could be part of another token within the haystack and you must thus take care to do something to guarantee a correct match. With FindToken disable the wildcards and that issue should be lessened or non-existent.


Further, with the wildcard support FindToken becomes easier than using a regular expression to access data which might be stored in a dictionary or tabular format (especially for those not comfortable with regex).

Code: Select all

$dict = "Blue;12|Red;15|Green;28";
$clr = Input("Input the name of a color:",,"red");
$idx = FindToken("$selectedColor;*", $dict, '|');
if ($idx > 0) {
  $clrData = GetToken($dict, $idx, "|");
  $clrCount = GetToken($clrData, 2, ";");
} else {
  $clrCount = "no";
}
Echo "There are $clrCount $clr item(s).";
Sure it doesn't do anything that couldn't already be done in another way, but it makes what it can do much easier.

Re: If (Var == "value1|value2|value3")

Posted: 20 Sep 2013 18:03
by admin
OK, makes sense. Maybe TokenPos or TokenIndex or GetTokenIndex would be a better name?

Re: If (Var == "value1|value2|value3")

Posted: 20 Sep 2013 18:22
by TheQwerty
admin wrote:OK, makes sense. Maybe TokenPos or TokenIndex or GetTokenIndex would be a better name?
What's wrong with FindToken?

I'm not really partial to the name..
GetTokenIndex is okay because that puts it near GetToken.
TokenPos is okay because it is similar in function to StrPos.
Not a fan of TokenIndex and not sure why. :?

Re: If (Var == "value1|value2|value3")

Posted: 20 Sep 2013 18:31
by admin
I prefer GetTokenIndex. In the meantime I coded the function in VB and it works nicely. Not sure if I find the time to add it to scripting before 13.00.

Re: If (Var == "value1|value2|value3")

Posted: 20 Sep 2013 18:50
by TheQwerty
admin wrote:Not sure if I find the time to add it to scripting before 13.00.
13.00.0001 it is! ;) :P

Re: If (Var == "value1|value2|value3")

Posted: 21 Sep 2013 10:48
by FluxTorpedoe
@TheQwerty
Thanks for clarifying your idea! I've had this need a few times too, so a GetTokenIndex would surely be a nice addition (when it comes)! :)

Re: If (Var == "value1|value2|value3")

Posted: 30 Sep 2013 16:09
by admin
TheQwerty wrote:
admin wrote:Not sure if I find the time to add it to scripting before 13.00.
13.00.0001 it is! ;) :P
No, it was v13.00.0003. :P