rightfield has asked for the wisdom of the Perl Monks concerning the following question:
I wrote some pseudo code and when Perl-izing it I discovered I can't seem to do what I had planned the way I had planned. I do not use Pearl often so I was wondering if you could tell me how you usually save values in a two-dimensional array as I have not found any examples.
Below is the partly Perl-ized portion of code where I had expected to loop through the two dimensions of an array called @summary, the first dimension would represent the week-of-the-month and the second the day-of-the-week. The value to be put in each location was the total number of orders placed on a particular day (the %total_opd hash) with each row across showing the day of the week and the columns were how many weeks the month spanned (a month can span 4, 5, or 6 weeks). Below is kind of what the output would look like: 0,0 means week 1 (partials count) and the second 0 is for Sunday. Next would be 0,1 for week 1, day 1=Monday.
There may or may not still be other bugs but the real issue is how would I stuff the array?
Thanks.
RF
| wk1 | wk2 | wk3 | wk4 | wk5 | |
| Sunday | 0,0 | 1,0 | 2,0 | 3,0 | 4,0 |
| Monday | 0,1 | 1,1 | 2,1 | 3,1 | 4,1 |
| Tuesday | 0,2 | 1,2 | 2,2 | ||
| Wednesday | 0,3 | ||||
| Thursday | 0,4 | ||||
| Friday | 0,5 | ||||
| Saturday | 0,6 | ||||
| Total | 0,7 |
# create data for small summary table of all retailers my $numWks = (($num_days - ( 7 - $firstDOW_curMonth )) / 7 ) + 1; if ( (($num_days - ( 7 - $firstDOW_curMonth )) % 7 ) ) { $numWks++; } my @summary[$numWks][7]; # !!! uh-oh this does not Perl-ize !!! # init the blank portion of $summary[][] if there is one my $temp = 0; while ( $temp < $firstDOW_curMonth ) { $summary[0][$temp++] = "--"; } my $wktotal; my $count=1; my $firstDOW = $firstDOW_curMonth; for ( my $wk = 0; $wk < $numwks; $wk++ ) { $wktotal = 0; for ( my $dw = $firstDOW; $dw < 7; $dw++ ) { $summary[$wk][$dw] += $total_opd{$count}; $wktotal += $total_opd{$count}; last if ($count++ == $num_days); } $firstDOW = 0; $summary[$wk][$7] = $wktotal; last if ($count > $num_days); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: pre-size a two-dimensional array
by ikegami (Patriarch) on Jul 11, 2007 at 21:58 UTC | |
by rightfield (Sexton) on Jul 12, 2007 at 03:44 UTC | |
by GrandFather (Saint) on Jul 12, 2007 at 04:23 UTC | |
by chrism01 (Friar) on Jul 12, 2007 at 04:16 UTC | |
by Anonymous Monk on Jul 16, 2007 at 06:07 UTC | |
|
Re: pre-size a two-dimensional array
by wind (Priest) on Jul 11, 2007 at 22:07 UTC | |
|
Re: pre-size a two-dimensional array
by Cristoforo (Curate) on Jul 19, 2007 at 00:19 UTC |