How to optimize checking for matching variables

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

How to optimize checking for matching variables

Post by bdeshi »

Hi, is there any way to check if a new variable's value matches any other variables without having to match against each variable one by one?

Say I have 1000 variables $var1, $var2, ...... $var1000 (with arbitrary values :wink: ). How do I check that a new $varNew matches any of those previous ones without doing:

Code: Select all

if ($varNew != $var1) && ..... &&($varNew != $var1000){
 //do something if no match found
}
??
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

nerdweed
Posts: 648
Joined: 25 Feb 2012 07:47
Location: XY64 Latest Beta , Win 11, 96dpi, 100%

Re: How to optimize checking for matching variables

Post by nerdweed »

What are you planning to do with 1000 variables. :?:

I trust you would be reading that from a file. If so you can read it in a single variable and then do a regexmatches on it.

You can than split that single variable across 1000 variables if need be.

PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: How to optimize checking for matching variables

Post by PeterH »

nerdweed wrote:What are you planning to do with 1000 variables. :?:

I trust you would be reading that from a file. If so you can read it in a single variable and then do a regexmatches on it.

You can than split that single variable across 1000 variables if need be.
...instead of regexmatches: could also be simply gettokenindex($varNew, $string, ...)

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How to optimize checking for matching variables

Post by bdeshi »

okay, maybe 1000 variables was an exaggeration :!:
Thanks, nerdweed, but the variables are dynamically generated.
PeterH wrote:gettokenindex($varNew, $string, ...)
Aha, thanks! I'll just do $string = $var1.$var2. ... .$var1000 and do ^that!
I can only wonder why I missed it, in fact I've often used a similar method to store random stuff... :oops:
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

nerdweed
Posts: 648
Joined: 25 Feb 2012 07:47
Location: XY64 Latest Beta , Win 11, 96dpi, 100%

Re: How to optimize checking for matching variables

Post by nerdweed »

Thanks PeterH.

I haven't used gettokenindex earlier but now I know where to look when I have a similar requirement.

Post Reply