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' };
In reply to Hash of Hashes from file by cipher
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |