WartHog369 has asked for the wisdom of the Perl Monks concerning the following question:
Greetings All,
This is my first posting/question. I am an ancient computer guy (punch cards and hand-wiring sort boards!) but relatively new to Perl.
Why does the line of code (65 or so lines below):
>>>>> foreach $hr (sort keys %{$ClubTotal ... <<<<<
not work? It does not sort, it does not appear to access the 'bottom key',
it does not return anything to $hr (I understand/realize that a list
should be returned from 'sort keys').
Can I - using 'only' my code access/retrieve all the data from %ClubTotal? Or, must I use Data::Walk or Data::Visitor or something similiar?
Many, many thanks for sharing your knowledge.
Thomas
* * * Begin program:
* * * Do some preprocessing of input data file, then process * * *
* * * 50,000+ lines of data that we are interested in: * * *
* * * Time of day by halfhour when a patron signs-in at club * * *
* * * This code builds the HASH: %ClubTotal * * *
Data::Dumper produces:$dataChunk = substr($_, 42, 17); # yields >06/05/2008 8:31a< from $_ if ($dataChunk =~ m|\d\d/\d\d/\d\d|) { $date = substr($dataChunk, 3, 2); # get date $hour = substr($dataChunk, 11, 2); # get hour $minute = substr($dataChunk, 14, 2); # get minutes $ampm = substr($dataChunk, 16, 1); # get am or pm if (($ampm eq "a") && ($hour <= 9)) {$hour = '0'.int($hour) ; } if (($ampm eq "p") && ($hour != 12)) {$hour = $hour + 12; } $ClubTotal{ 'DayOfMonth'=> $date }{'Date'} = $date ; $ClubTotal{ 'DayOfMonth'=> $date }{'TotalPerDay'} += 1 ; $ClubTotal{ 'DayOfMonth'=> $date }{ 'HourSignIn' => $hour }{'Hour'} += $hour ; $ClubTotal{ 'DayOfMonth'=> $date }{ 'HourSignIn' => $hour }{'TotPerH +our'} += 1 ; if ($minute <= 29) { $ClubTotal{ 'DayOfMonth'=> $date }->{ 'HourSignIn' => $hour }->{'H +alfHour00'} += 1 ; } else { $ClubTotal{ 'DayOfMonth'=> $date }->{ 'HourSignIn' => $hour }->{'H +alfHour30'} += 1 ; } #* * * Do some extra stuff here, finished with input file * * * #* * * Call: sub SortBuildOutput{ * * * sub SortBuildOutput{ #%ClubTotal hash #DayOfMonthxx hash #HourSignInxx hash #TotPerHour scalar #HalfHour00 scalar #HalfHour30 scalar #Hour scalar my $day; my $hr; my @hr; foreach $day (sort keys %ClubTotal) { >>>>> foreach $hr (sort keys %{$ClubTotal{'$day'}{'HourSignIn'} } ) { + <<<<< print $ClubTotal{$day}->{'Date'} . ": " . $ClubTotal{$day}->{'To +talPerDay'} . "\n"; # This TASK: Aggregate Data for end of month summary: # %ClubTotal = ( # 'DayOfMonth26' => { # 'HourSignIn13' => { # 'HalfHour30' => 7, # 'Hour' => 13, # 'HalfHour00' => 11, # 'TotPerHour' => 18 # } # } # to build a record for input to a spreadsheet } # END: foreach my $hr (sort keys %{$ClubTotal{'$day'}{HourSignI +n}}) { } # END: foreach my $day (sort keys %ClubTotal) { } #END: sub SortBuildOutput{
%ClubTotal = ( 'DayOfMonth26' => { 'HourSignIn13' => { 'HalfHour30' => 13, 'Hour' => 13, 'HalfHour00' => 10, 'TotPerHour' => 23 }, 'HourSignIn12' => { 'HalfHour30' => 6, 'Hour' => 12, 'HalfHour00' => 8, 'TotPerHour' => 14 }, 'HourSignIn10' => { 'HalfHour30' => 20, 'Hour' => 10, 'HalfHour00' => 19, 'TotPerHour' => 39 'TotalPerDay' => 251, 'Date' => '26', 'DayOfMonth11' => { 'HourSignIn13' => { 'HalfHour30' => 7, 'Hour' => 13, 'HalfHour00' => 11, 'TotPerHour' => 18 }, 'HourSignIn10' => { 'HalfHour30' => 12, 'Hour' => 10, 'HalfHour00' => 11, 'TotPerHour' => 23 },
and so on for all 30 days of the month to produce cumulative totals of all data/scalar items.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Brain muchly befuddled by nested hashes
by ysth (Canon) on Nov 24, 2008 at 04:57 UTC | |
by graff (Chancellor) on Nov 24, 2008 at 05:30 UTC | |
by ysth (Canon) on Nov 24, 2008 at 06:05 UTC | |
|
Re: Brain muchly befuddled by nested hashes
by ptoulis (Scribe) on Nov 24, 2008 at 08:28 UTC | |
by WartHog369 (Novice) on Nov 24, 2008 at 17:35 UTC | |
by ptoulis (Scribe) on Nov 24, 2008 at 18:55 UTC | |
|
Re: Brain muchly befuddled by nested hashes
by Cristoforo (Curate) on Nov 25, 2008 at 00:19 UTC | |
|
Re: Brain muchly befuddled by nested hashes
by johngg (Canon) on Nov 25, 2008 at 23:55 UTC | |
by WartHog369 (Novice) on Nov 26, 2008 at 06:04 UTC | |
|
Re: Brain muchly befuddled by nested hashes
by wol (Hermit) on Nov 25, 2008 at 17:48 UTC |