http://www.perlmonks.org?node_id=1003729

jaffinito43 has asked for the wisdom of the Perl Monks concerning the following question:

I have a script where each time thru a while loop, I need to compare a variable to a hash key to see if its there or not. If it is there, then I need to compare the value of that key to the previous value to see if its greater or not. Then if both criteria are met, the script should perform a print statement, however nothing is printing. Is it possible to check a variable against all the keys in a hash?

####THIS CODE IS ALL IN A WHILE LOOP SO THE VALUE #### OF $remoteIP MAY BE CHANGING EACH TIME #sets the hash for first time thru the loop if (! exists ($hash{$remoteIP})){ $hash{$remoteIP} = 99999999999999999999999999999 } print $hash{$remoteIP}."\n"; # checks if epoch time of $remoteIP is less greater #than the previous time set for that variable. # if so, it updates it so the value of the key # is the earliest time if ($hash{$remoteIP} > $logDate){ $hash{$remoteIP} = $logDate; } print $hash{$remoteIP}."\n"; print %hash; my @k = keys %hash; my @v = values %hash; if ($remoteIP ~~ @k){ if ($hash{$remoteIP} > $logDate){ print "YAY IT WORKED"; } }

This script does not print "YAY IT WORKED", though it should several times. So I also tried this instead of using $logDate in the last if statement:

if ($remoteIP ~~ @k){ if ($hash{$remoteIP} > $v[$remoteIP]){ print "YAY IT WORKED"; } }

This time, it prints YAY IT WORKED each time thru the loop because I believe $v$remoteIP is equal to 0. Pretty much Im trying to see whether or not the keys match, and if they do, whether or not the current value in the loop is less than that of the previously stored value for that key