Page 1 of 1
How to optimize checking for matching variables
Posted: 23 May 2014 13:45
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

). 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
}
??
Re: How to optimize checking for matching variables
Posted: 23 May 2014 14:24
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.
Re: How to optimize checking for matching variables
Posted: 23 May 2014 14:35
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, ...)
Re: How to optimize checking for matching variables
Posted: 23 May 2014 14:45
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...
Re: How to optimize checking for matching variables
Posted: 23 May 2014 17:01
by nerdweed
Thanks PeterH.
I haven't used gettokenindex earlier but now I know where to look when I have a similar requirement.