Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Aaaarghh...hashes of hashes..again :(

by viffer (Beadle)
on Mar 23, 2011 at 04:08 UTC ( #894928=note: print w/replies, xml ) Need Help??


in reply to Re: Aaaarghh...hashes of hashes..again :(
in thread Aaaarghh...hashes of hashes..again :(

Cheers
Data is entered via a web page and the votes get written to a file, looking like
01,01,02,03,23/03/2011 12:27:41 01,03,05,08,23/03/2011 12:37:41 01,02,05,07,23/03/2011 12:47:41 01,03,02,03,23/03/2011 12:57:41 01,01,06,08,23/03/2011 12:67:41 02,01,02,03,24/03/2011 12:27:41 02,03,05,08,24/03/2011 12:37:41 02,02,05,07,24/03/2011 12:47:41 02,03,02,03,24/03/2011 12:57:41 02,01,06,08,24/03/2011 12:67:41
where:
the first value is the game, the second, third and fourth values are the players that have been voted for. The last is date and time.

So in the above example, 5 votes have been made for both games one and two.

In the first record
01,01,02,03,23/03/2011 12:27:41
For game 01, player 01 would get 3 points, player 02 would get 2 points and player 03 would get 1 point
. I had tried to make a smaller version of the code (using only three tables rather than 25) but...still left me completely non plussed!

Replies are listed 'Best First'.
Re^3: Aaaarghh...hashes of hashes..again :(
by GrandFather (Saint) on Mar 23, 2011 at 04:49 UTC

    If I understand what you want then the following may help:

    use strict; use warnings; my %totals; while (defined(my $line = <DATA>)) { chomp $line; my ($game, $first, $second, $third, $dates) = split /,/, $line; $totals{$game}{$first} += 3; $totals{$game}{$second} += 2; $totals{$game}{$third} += 1; } printResults($_, $totals{$_}) for sort keys %totals; sub printResults { my ($gameName, $gameTotals) = @_; my %byTotal; push @{$byTotal{$gameTotals->{$_}}}, $_ for keys %$gameTotals; my @scores = sort {$b <=> $a} keys %byTotal; my @place = qw{first second third}; @scores = grep {defined} @scores[0 .. 2]; print "Results for $gameName\n"; for my $score (@scores) { print "$place[0] with $score points: ", join(', ', @{$byTotal{ +$score}}), "\n"; shift @place; } print "\n"; } __DATA__ 01,01,02,03,23/03/2011 12:27:41 01,03,05,08,23/03/2011 12:37:41 01,02,05,07,23/03/2011 12:47:41 01,03,02,03,23/03/2011 12:57:41 01,01,06,08,23/03/2011 12:67:41 02,01,02,03,24/03/2011 12:27:41 02,08,05,08,24/03/2011 12:37:41 02,02,05,07,24/03/2011 12:47:41 02,03,02,03,24/03/2011 12:57:41 02,09,06,08,24/03/2011 12:67:41

    Prints:

    Results for 01 first with 8 points: 03 second with 7 points: 02 third with 6 points: 01 Results for 02 first with 7 points: 02 second with 5 points: 08, 03 third with 4 points: 05
    True laziness is hard work
      Thank you :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://894928]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2022-05-25 17:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (90 votes). Check out past polls.

    Notices?