in reply to Re^7: Hash of Hashes from file
in thread Hash of Hashes from file
Think I got it this time! I used the data structure that scorpio17 used (I printed out the dump here also).
Output:
And here is the code:C:\Old_Data\perlp>perl t33.pl $VAR1 = { 'john' => { 'site' => [ 'www.yahoo.com', 'www.yahoo.com', 'www.yahoo.com', 'www.facebook.com' ], 'type' => [ 'Entertainment', 'Entertainment', 'Entertainment', 'Social Networking' ] }, 'mike' => { 'site' => [ 'www.google.com' ], 'type' => [ 'Search Engines' ] }, 'david' => { 'site' => [ 'www.facebook.com' ], 'type' => [ 'Social Networking' ] } }; david Website: www.facebook.com, Category: Social Networking john Website: www.yahoo.com, Category: Entertainment Website: www.yahoo.com, Category: Entertainment Website: www.yahoo.com, Category: Entertainment Website: www.facebook.com, Category: Social Networking mike Website: www.google.com, Category: Search Engines Name: john Website Count www.yahoo.com 3 www.facebook.com 1 Type Count Entertainment 3 Social Networking 1 Name: mike Website Count www.google.com 1 Type Count Search Engines 1 Name: david Website Count www.facebook.com 1 Type Count Social Networking 1 C:\Old_Data\perlp>
Hope this helps, Chris#!/usr/bin/perl use strict; use warnings; my %data; while (<DATA>) { my ($user, $site, $cat) = /"([^"]+)"/g; push @{ $data{$user}{site} }, $site; push @{ $data{$user}{type} }, $cat; } for my $user (sort keys %data) { my $site_ary = $data{$user}{site}; my $type_ary = $data{$user}{type}; print $user, "\n"; for my $i (0 .. $#$site_ary) { printf "\tWebsite: %s, Category: %s\n", $site_ary->[$i], $type +_ary->[$i]; } } print "\n\n"; for my $user (sort by_count_desc keys %data) { my $site_ary = $data{$user}{site}; my $type_ary = $data{$user}{type}; my (%site_cnt, %type_cnt); $site_cnt{$_}++ for @$site_ary; $type_cnt{$_}++ for @$type_ary; print "Name: $user\n\tWebsite Count\n"; for my $site (sort {$site_cnt{$b} <=> $site_cnt{$a}} keys %site_cn +t) { printf "\t%-20s%d\n", $site, $site_cnt{$site}; } print "\n"; print "\tType Count\n"; for my $type (sort {$type_cnt{$b} <=> $type_cnt{$a}} keys %type_cn +t) { printf "\t%-20s%d\n", $type, $type_cnt{$type}; } print "\n\n"; } sub by_count_desc { @{$data{$b}{site}} <=> @{$data{$a}{site}}; } __DATA__ user="john" website="www.yahoo.com" type="Entertainment" user="john" website="www.yahoo.com" type="Entertainment" 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"
Update: Added sub by_count_desc.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^9: Hash of Hashes from file
by Cristoforo (Curate) on Apr 14, 2012 at 14:54 UTC | |
Re^9: Hash of Hashes from file
by cipher (Acolyte) on Apr 09, 2012 at 12:04 UTC |
In Section
Seekers of Perl Wisdom