in reply to pre-size a two-dimensional array
#!/usr/bin/perl use strict; use warnings; use Date::Simple 'ymd'; use Text::Table; #Set up an example hash of orders placed daily (for the month of July) my %total_opd; @total_opd{1..30} = (12..40, 1000); # Use July 2007 for this example my ($y, $m, $d) = (2007, 7, 1); my @summary = [qw/Sun Mon Tue Wed Thu Fri Sat Total/ ]; my ($row, $col) = 1; for (my $date = ymd($y, $m, $d); $date->month == $m; $date++) { $col = $date->day_of_week; # weekdays are 0 - 6 # if $total_opd{$date->day} not exists or is undefined this day, s +et to 0 $summary[$row][7] += $summary[$row][$col] = $total_opd{$date->day} + || 0; ++$row if $col==6; } my $num_weeks = @summary - 1; my @head = (" ", map "Wk" . $_, 1..$num_weeks); my @new = transpose(@summary); my $tb = Text::Table->new( map {title => $_, align_title => 'right'}, +@head)->load(@new); print $tb; sub transpose { my (@summary, @new) = @_; for my $r (0..$#summary) { for my $c (0..$#{ $summary[$r]}) { $new[$c][$r] = $summary[$r][$c]; } } return @new; } __END__ ***prints output Wk1 Wk2 Wk3 Wk4 Wk5 Sun 12 19 26 33 40 Mon 13 20 27 34 1000 Tue 14 21 28 35 0 Wed 15 22 29 36 Thu 16 23 30 37 Fri 17 24 31 38 Sat 18 25 32 39 Total 105 154 203 252 1040
|
|---|