cipher has asked for the wisdom of the Perl Monks concerning the following question:
I have been learning Perl for quite sometime now but still concept of hashes is not clear to me.
I want to print all websites and categories visited by each unique user.File format: user=john website="www.yahoo.com" type="Entertainment" user=david website="www.facebook.com" type="Social Networking" user=john website="www.facebook.com" type="Social Networking" user=mike website="www.google.com" type="Search Engines"
Something like this.
My scriptjohn => [Website: www.yahoo.com , Category: Entertainment, [Website: www.facebook.com, Category:Social Networking] david =>[Website: www.facebook.com, Category:Social Networking] mike=>[Website: www.google.com, Category:Search Engines]
My script does not list all websites for user John. Any help is appreciated.open FILE, "new.log" or die; my %hoh; while (<FILE>) { if ($_ =~ /user="([^"]+)/) {$user = $1; } if ($_ =~ /website="([^"]+)/) {$website = $1; } if ($_ =~ /type="([^"]+)/) {$type = $1; } # shows only one website per user $hoh{$user}->{'Website'} = $site; $hoh{$user}->{'Category'} = $cat; } use Data::Dumper; print Dumper (%hoh); Output: $VAR1 = 'john'; $VAR2 = { 'Type' => 'Social Networking', 'Website' => 'www.facebook.com' }; $VAR3 = 'mike'; $VAR4 = { 'Type' => 'Search Engines', 'Website' => 'www.google.com' }; $VAR5 = 'david'; $VAR6 = { 'Type' => 'Social Networking', 'Website' => 'www.facebook.com' };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Hash of Hashes from file
by scorpio17 (Canon) on Apr 03, 2012 at 13:04 UTC | |
by cipher (Acolyte) on Apr 03, 2012 at 13:09 UTC | |
by tobyink (Canon) on Apr 03, 2012 at 13:32 UTC | |
by cipher (Acolyte) on Apr 03, 2012 at 14:40 UTC | |
by cipher (Acolyte) on Apr 03, 2012 at 15:04 UTC | |
by scorpio17 (Canon) on Apr 03, 2012 at 13:36 UTC | |
by cipher (Acolyte) on Apr 03, 2012 at 14:31 UTC | |
by scorpio17 (Canon) on Apr 03, 2012 at 15:19 UTC | |
| |
by Cristoforo (Curate) on Apr 03, 2012 at 20:57 UTC | |
|