Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I got the following output:
C:\Old_Data\perlp>perl t33.pl 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
From this 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"
Notice that there are quotes surrounding every field. The regular expression that captures these fields from the file would need to be changed if thats not the case.

In my program I use 2 hashes - one to count the number of sites visited by each user, %count, and one to count each address and category (by user), %data. It seems to work OK for this small data set.

#!/usr/bin/perl use strict; use warnings; my (%data, %count); while (<DATA>) { my ($user, $site, $cat) = /"([^"]+)"/g; $data{$user}{ qq{$site$;$cat} }++; $count{$user}++; } for my $user (sort keys %data) { my $href = $data{$user}; print $user, "\n"; for my $key (keys %$href) { my $str = sprintf "\tWebsite: %s, Category: %s\n", split /$;/, + $key; print $str x $href->{$key}; } } my @ordered = sort {$count{$b} <=> $count{$a}} keys %count; print "\n\n"; for my $user (@ordered) { my $href = $data{$user}; print "Name: $user\n\tWebsite Count\n"; for my $key (sort {$href->{$b} <=> $href->{$a}} keys %$href) { printf "\t%-20s%d\n", (split /$;/, $key)[0], $href->{$key}; } print "\n"; print "\tType Count\n"; for my $key (sort {$href->{$b} <=> $href->{$a}} keys %$href) { printf "\t%-20s%d\n", (split /$;/, $key)[1], $href->{$key}; } print "\n\n"; }
The line $data{$user}{ qq{$site$;$cat} }++; uses a 'compound' key ($site and $cat joined by $;).

Here is a dump of %data.

$VAR1 = { 'john' => { 'www.yahoo.com‡˜Entertainment' => 3, 'www.facebook.com‡˜Social Networking' => 1 }, 'mike' => { 'www.google.com‡˜Search Engines' => 1 }, 'david' => { 'www.facebook.com‡˜Social Networking' => } };

Update: Whoops, that doesn't count the categories correctly :-(
If there was another site with the same category, it wouldn't be totaled with the same category from another site.


In reply to Re^7: 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 wandering the Monastery: (5)
As of 2024-04-24 19:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found