James marks wrote:
Hello folks,

Hello,

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...

Do you mean something like this:

$ perl -e'
my %hash = (
    q[192.168.1.1] => {
        user_agent => q[...GoogleBot...],
        user_type  => q[robot],
        },
    q[192.168.1.3] => {
        user_agent => q[...Linux...Firefox...],
        user_type  => q[human],
        },
    q[192.168.1.4] => {
        user_agent => q[...Yahoo! Slurp...],
        user_type  => q[robot],
        },
    q[192.168.1.5] => {
        user_agent => q[...Windows...MSIE...],
        user_type  => q[human],
        },
    );

print "$_ $hash{$_}{user_agent}\n" for grep $hash{$_}{user_type} eq q[robot], keys %hash;
'
192.168.1.4 ...Yahoo! Slurp...
192.168.1.1 ...GoogleBot...




John
--
use Perl;
program
fulfillment

--
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