Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Comparing a value to hash key

by NetWallah (Abbot)
on Nov 14, 2012 at 04:53 UTC ( #1003750=note: print w/replies, xml ) Need Help??


in reply to Comparing a value to hash key

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003750]
help
Chatterbox?
[Corion]: Meh, first round of escalations for me not wanting to fix in production what a project has mismanaged. Now another project, which eats up all the resources until end of this year wants to take that task and put it on my list of things as well.
[Corion]: So now there will be the fun of me explaining to the project that $other_project had low priority because $project has high priority. If $project picks up the task from $other_project, they also will need to reprioritize my tasks to get that done...
[Corion]: ... of course at the cost of something else on my list of tasks for $project, as there only is a finite amount of days until $deadline.

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2017-08-17 12:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (287 votes). Check out past polls.

    Notices?