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
and updated the hashes viamy %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, );
I've changed this to not having the hashes declared at all and populating them viawhile (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; } }
What I was previously doing (25 times) waswhile (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; }
I've changed this to%totals = %G01totals; 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.for (my $i=1;$i<3;$i++) { $print_heading = 1; $game_index = $i; create_tables ($print_heading, $game_index, %totals); }
I did have
Which worked perfectly - when I knew what hash I was getting - since i was calling it for each hash. I've now tried usingsub 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 ); }
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 atsub 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 ); }
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
In reply to Aaaarghh...hashes of hashes..again :( by viffer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |