> Hello folks,
> 
> I'm wondering if any of you could help me with a code problem? I'm 
> relatively new to Perl, coming to it from AppleScript, and I've written 
> a Perl script that parses my server access_log and creates a hash of 
> hashes that looks essentially like this:
> 
> 192.168.1.1 =
>                               $user_agent = "...GoogleBot..."
>                               $user_type  = "robot"
> 192.168.1.3 =
>                               $user_agent = "...Linux...Firefox..."
>                               $user_type  = "human"
> 192.168.1.4 =
>                               $user_agent = "...Yahoo! Slurp..."
>                               $user_type  = "robot"
> 192.168.1.5 =
>                               $user_agent = "...Windows...MSIE..."
>                               $user_type  = "human"
> etc...
> 
> So, in the first hash, the ip address is the key and the value is the 
> second, nested hash.
> 
> I want to traverse that hash of hashes to extract the user agent of 
> each IP whose user type is "robot" like this:
> 
> LIST OF ROBOTS VISITING SITE:
> 192.168.1.1 "...GoogleBot..."
> 192.168.1.4 "...Yahoo! Slurp..."
> etc...
> 
> I've been struggling with various versions of a foreach loop but can't 
> seem to get it to work. Is it possible to traverse a hash of hashes and 
> extract data based on a value from a key/value pair that is nested in 
> another hash?
> 

What you are doing would be much clearer if you included actual code.
My instincts tell me you might be building your hash-of-hashrefs
incorrectly, which case my example here will be of no help to you.

Use Data::Dumper to make sure your hash of hashrefs is what you think
it is ... 

................................... BEGIN PERL CODE ....................
#!/usr/bin/perl
# File jm.pl

use strict;
use warnings;

my %log= ( '192.168.1.1' => { user_agent => '...GoogleBot...',
                              user_type => 'robot' },
           '192.168.1.3' => { user_agent => '...Linux...Firefox...',
                              user_type => 'human' },
           '192.168.1.5' => { user_agent => '...Windows...MSIE...',
                              user_type => 'human' } );

foreach my $key (keys %log ) {
    print "$key \"$log{$key}{user_agent}\"\n";
}

.................................... END PERL CODE .....................

Produces the follwoing output:

lawrence  /tmp > perl ./jm.pl
192.168.1.1 "...GoogleBot..."
192.168.1.3 "...Linux...Firefox..."
192.168.1.5 "...Windows...MSIE..."

Another place that could be giving you grief is trying to embed dollar
signs in your hash keys - I'm sure it's possible, but it causes much
grief and pain and anguish.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
        Lawrence Statton - [EMAIL PROTECTED] s/aba/c/g
Computer  software  consists of  only  two  components: ones  and
zeros, in roughly equal proportions.   All that is required is to
sort them into the correct order.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to