Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Access the Keys of a Hash Reference Without a For Loop

by Dru (Hermit)
on Nov 01, 2010 at 16:56 UTC ( [id://868810]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

I have been needing your help a lot lately. This is a followup question to Is Using Threads Slower Than Not Using Threads?. I am trying to optimize my code to deal with a large list of ip addresses against a large text file.

Could you please school me on how to access the keys of a hash reference without using a for loop? I thought exists $ipsAll->{key}) was the way, but that appears to be how to access the values of the keys.
use warnings; use strict; use Regexp::Common qw /net/; my $fw_file = 'tmp.log'; my @allIps = qw(10.8.1.5 10.8.1.6 10.8.1.7); my %ipsMatch = map {$_ => 1} @allIps; my @infectedIps = check_fw_logs(\%ipsMatch, $fw_file); sub check_fw_logs { # Sample Line to match Oct 31 23:58:12 x.x.x.x Oct 31 2010 23:58:14: <br>%PIX-5-106100: access-list INBOUND denied udp <br>outside/10.158.53.2(12143) -> dmz/192.168.19.11(53) <br> hit-cnt 8 300-second interval [0x6be0682a, 0x0] my $ipsAll, $file = @_; my (@infectedIps); open (FILE, $file) or croak("Can't open $file: $!\n"); while(<FILE>){ my $sub = substr($_, 105, 40); if ($sub =~ /(\d+\.\d+\.\d+\.\d+)\(?:\d+\) -> outside\/(\d+\.\d+\.\d+\.\d+)/ && exists $ipsAll->{$1}){ push(@infectedIps,$1); } } return @infectedIps; }

Thanks,
Dru

Perl, the Leatherman of Programming languages. - qazwart

Replies are listed 'Best First'.
Re: Access the Keys of a Hash Reference Without a For Loop
by umasuresh (Hermit) on Nov 01, 2010 at 17:47 UTC
    I am not sure if this is what you are looking for:
    my @keys = keys %{$hash_ref};
Re: Access the Keys of a Hash Reference Without a For Loop
by wink (Scribe) on Nov 01, 2010 at 18:28 UTC
    exists $ipsAll->{$key} will return 1 (true) if the key exists. If you need to get a list of the keys, umasuresh's answer is what you want. Also, be careful about using exists on nested hash references, since the -> operator will create intervening values.
    my %hash = ('foo' => 'foo'); my $ref = \%hash; print exists $ref->{'foo'}; #Prints 1 (true) print exists $ref->{'bar'}->{'baz'}; #Prints nothing (false) print exists $ref->{'bar'}; #Prints 1 (true)
      Thanks wink, I was just about to post to say I realized my code was actually correct and it will print out 1 (meaning it's true) if there was a match, basically what you said.

      Thanks,
      Dru

      Perl, the Leatherman of Programming languages. - qazwart
Re: Access the Keys of a Hash Reference Without a For Loop
by Jim (Curate) on Nov 01, 2010 at 17:58 UTC
    #!perl use strict; use warnings; my $state_capital_of; $state_capital_of->{'Alabama'} = 'Montgomery'; $state_capital_of->{'Alaska'} = 'Juneau'; $state_capital_of->{'Arizona'} = 'Phoenix'; $state_capital_of->{'Arkansas'} = 'Little Rock'; $state_capital_of->{'California'} = 'Sacramento'; print "$state_capital_of->{'Alabama'}\n"; print "$state_capital_of->{'Alaska'}\n"; print "$state_capital_of->{'Arizona'}\n"; print "$state_capital_of->{'Arkansas'}\n"; my $state = 'California'; print "$state_capital_of->{$state}\n" if exists $state_capital_of->{$state};

    This prints

    Montgomery
    Juneau
    Phoenix
    Little Rock
    Sacramento
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://868810]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-18 23:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found