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

Replies are listed 'Best First'.
Re: Comparing a value to hash key
by NetWallah (Canon) on Nov 14, 2012 at 04:53 UTC
    Is it possible to check a variable against all the keys in a hash
    The question suggests you do not understand the fundamental property of a hash.

    The primary use of a hash is for a quick and simple lookup of a 'key', by indexing, using curly-braces.

    The first line of your code:

    if (! exists ($hash{$remoteIP})){
    does exactly that - looks up the value of $remoteIP, and can be thought of as comparing with every possible key in %hash, and returning 'true' if the matching one exists.

    Your later comparison:

    if ($remoteIP ~~ @k){
    is a more expensive way of doing the same comparison that the first line does.

                 "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."           -Confucius

Re: Comparing a value to hash key
by frozenwithjoy (Priest) on Nov 14, 2012 at 02:10 UTC
    This script does not print "YAY IT WORKED", though it should several times.
    OK, so you have this:
    if ( $hash{$remoteIP} > $logDate ) { $hash{$remoteIP} = $logDate; }

    ...after which, $hash{$remoteIP} will always be less than or equal to $logDate, right?

    So, (unless I'm missing something) the problem is that you are only printing "YAY..." if ($hash{$remoteIP} > $logDate), which (as a result of your earlier if statement) is never true.



    EDIT:
    As for why your substitution works, my guess is that @v might be undefined at $v[$remoteIP]. Do you have use warnings; in your script? If so, do you get a "Use of uninitialized value ..." error? I might throw in something like the following to make sure what you think is happening really is happening, because I don't think $v[$remoteIP] is doing what you think it is doing:

    say "\$remote: $remote"; say "\$v[\$remoteIP]: $v[$remoteIP]"; say "\$hash{\$remoteIP}: $hash{$remoteIP}";