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


In reply to Comparing a value to hash key by jaffinito43

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.