Hi people of wisdom.
I'm REALLY struggling to get my head around the concept of hashes of hashes.
I've written a little script to enable members of my football team to vote for their player of the match on line. Then add up the total votes and allocate 3 points to the guy with the most votes, 2 to the 2nd most and 1 to the third most - and some less than nifty way of calculating when people have the same number of votes, but I digress...

I have ended up with 25 hashes, one per game. I KNOW I can make it one hash - and have worked out how to write the hashes 'on the fly'. The problem I'm now having, is trying to access the individual hashes within the overall hash. I was simply copying each one of the 25 hashes into another hash then calling the same subroutine each time. There HAS to be an easier way.

I originally had

my %G01totals = ( '01' => 0, '02' => 0, '03' => 0, '04' => 0, '05' => 0, '06' => 0, +'07' => 0, '08' => 0, '09' => 0, '10' => 0, '11' => 0, '12' => 0, '13' => 0, +'14' => 0, '15' => 0, '16' => 0, '17' => 0, '18' => 0, '19' => 0, '20' => 0, +'21' => 0, ); my %G02totals = ( '01' => 0, '02' => 0, '03' => 0, '04' => 0, '05' => 0, '06' => 0, +'07' => 0, '08' => 0, '09' => 0, '10' => 0, '11' => 0, '12' => 0, '13' => 0, +'14' => 0, '15' => 0, '16' => 0, '17' => 0, '18' => 0, '19' => 0, '20' => 0, +'21' => 0, ); my %G03totals = ( '01' => 0, '02' => 0, '03' => 0, '04' => 0, '05' => 0, '06' => 0, +'07' => 0, '08' => 0, '09' => 0, '10' => 0, '11' => 0, '12' => 0, '13' => 0, +'14' => 0, '15' => 0, '16' => 0, '17' => 0, '18' => 0, '19' => 0, '20' => 0, +'21' => 0, );
and updated the hashes via
while (my $line = <$file>) { chomp ($line); my ($in_game, $in_three, $in_two, $in_one) = split (',', $line) +; if ($in_game eq 'G01') { $G01totals{$in_three} += 3; $G01totals{$in_two} += 2; $G01totals{$in_one} += 1; } if ($in_game eq 'G02') { $G02totals{$in_three} += 3; $G02totals{$in_two} += 2; $G02totals{$in_one} += 1; } if ($in_game eq 'G03') { $G03totals{$in_three} += 3; $G03totals{$in_two} += 2; $G03totals{$in_one} += 1; } }
I've changed this to not having the hashes declared at all and populating them via
while (my $line = <$file>) { chomp ($line); my ($in_game, $three, $two, $one) = split (',', $line); $totals{$in_game}{$three} += 3; $totals{$in_game}{$two} += 2; $totals{$in_game}{$one} += 1; }
What I was previously doing (25 times) was
%totals = %G01totals; create_tables ($print_heading, $game_index, %totals);
I've changed this to
for (my $i=1;$i<3;$i++) { $print_heading = 1; $game_index = $i; create_tables ($print_heading, $game_index, %totals); }
But in my create_tables subroutine I'm really having problems trying to work out how to access the hash.

I did have

sub create_tables { my ($heading_flag, $game_no, %totals) = @_; if( %totals) { open( $out_file, '>', $obtain_321_file ) or die "G1 Open output file $obtain_321_file failed $!"; foreach $key (sort sortvalue (keys(%totals))) { if (! $totals{$key} == 0) { print ($out_file "$totals{$key},$players{$key}\n"); } } close ( $out_file ); calculate_overall_results(); close ( $out_file ); }
Which worked perfectly - when I knew what hash I was getting - since i was calling it for each hash. I've now tried using
sub create_tables { my ($heading_flag, $game_no, %totals) = @_; if( %totals) { open( $out_file, '>', $obtain_321_file ) or die "G1 Open output file $obtain_321_file failed $!"; foreach $game_no (sort keys %totals) { foreach my $key (sort keys %{$totals{$game_no}}) { if (! $totals{$game_no} == 0) { print ($out_file "$totals{$game_no}{$key},$players{$ +key}\n"); } } } close ( $out_file ); calculate_overall_results(); close ( $out_file ); }
and basically that's complete bollocks! I've lost count of the number of variations I've tried. The code in it's entirety can be found at

Working long version:
home.iprimus.com.au/viffer/white_vote_cur_standings.cgi.txt
Attempt to reduce hashes
home.iprimus.com.au/viffer/white_vote_results_new.cgi.txt

Abridged version of entire code
home.iprimus.com.au/viffer/voting_original.cgi.txt
Attempt to fix abridged version
home.iprimus.com.au/viffer/voting_updated.cgi.txt

If anyone could let me know how I can read each hash (in the right order) individually, I would be extremely grateful. Thanks Kev

In reply to Aaaarghh...hashes of hashes..again :( by viffer

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.