Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Think I got it this time! I used the data structure that scorpio17 used (I printed out the dump here also). Output:
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>
And here is the code:
#!/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"
Hope this helps, Chris

Update: Added sub by_count_desc.


In reply to Re^8: Hash of Hashes from file by Cristoforo
in thread Hash of Hashes from file by cipher

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-24 20:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found