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

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
binocular222
Posts: 1416
Joined: 04 Nov 2008 05:35
Location: Hanoi, Vietnam

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

Post 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?
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

Marco
Posts: 2347
Joined: 27 Jun 2011 15:20

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

Post by Marco »

Shouldn't it be

Code: Select all

If("<curext>" == "docx" | "<curext>" == "doc" | "<curext>" == "xlsx" | "<curext>" == "xls") {msg ok}
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

highend
Posts: 13339
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

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

Post 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
One of my scripts helped you out? Please donate via Paypal

binocular222
Posts: 1416
Joined: 04 Nov 2008 05:35
Location: Hanoi, Vietnam

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

Post 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
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

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

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

FluxTorpedoe
Posts: 855
Joined: 05 Oct 2011 13:15

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

Post 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... :)

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

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

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

admin
Site Admin
Posts: 60635
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

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

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

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

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

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

admin
Site Admin
Posts: 60635
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

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

Post by admin »

OK, makes sense. Maybe TokenPos or TokenIndex or GetTokenIndex would be a better name?

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

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

Post 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. :?

admin
Site Admin
Posts: 60635
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

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

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

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

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

Post 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

FluxTorpedoe
Posts: 855
Joined: 05 Oct 2011 13:15

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

Post 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)! :)

admin
Site Admin
Posts: 60635
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

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

Post 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

Post Reply