Here is a solution using 2 helpful modules. Date::Simple takes care of all the date arithmetic and Text::Table formats the output (automatically sizing the columns to fit the data).
Chris
#!/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

In reply to Re: pre-size a two-dimensional array by Cristoforo
in thread pre-size a two-dimensional array by rightfield

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.