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

Ok, let's go right back to the start. Where does your your input data come from and what does it look like? What I've seen so far suggests and array of games where each element contains an array of counts - no hashes at all!

Not that is likely to fix your problem because you still have the same nested structure, but an array of arrays does seem at first glance to be a more natural fit to what I infer your data to be. The important thing about hashes and arrays is that they only store scalar values, but a reference is a scalar so you can store a reference to an array or hash in the element of an array or hash - hence you can have nested data structures.

Note that it often helps to write a small test script that uses a little test data and prints a simple version of the results to figure out how to solve the tricky bit in isolation. Aside from making testing easier, that's also a good piece of code to use when you want input to solving the problem from others (like, oh I don't know, um Perl monks maybe).

True laziness is hard work
  • Comment on Re: Aaaarghh...hashes of hashes..again :(

Replies are listed 'Best First'.
Re^2: Aaaarghh...hashes of hashes..again :(
by viffer (Beadle) on Mar 23, 2011 at 04:08 UTC
    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!

      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 :)