[Pdx-pm] traversing (and accessing values in) a hash of hashes

James marks jamarks at jamarks.com
Sat Dec 11 17:55:41 CST 2004


Hello folks,

I've been to a Portland Perl Mongers meeting only once so I'm sure you 
don't remember me but 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?

I could just create two hashes, one for humans and one for robots, but 
one hash that contains all the information seemed to be a more elegant 
solution...

Any help would be greatly appreciated.

Thanks,

James
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 1487 bytes
Desc: not available
Url : http://mail.pm.org/archives/pdx-pm-list/attachments/20041211/a3e00e68/attachment.bin


More information about the Pdx-pm-list mailing list