Aboveyou has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Can anyone please tell me how can i modify the script below so that i can move the print result into an array so that it will look this: ex, month is june 2009 @montharr = ("",1..30); or july @montharr = ("", "", "",1..31)
#!/usr/bin/perl -w use strict; use Calendar::Simple; my @months = qw(January February March April May June July August September October November December); my $mon = shift || (localtime)[4] + 1; my $yr = shift || (localtime)[5] + 1900; my @month = calendar($mon, $yr); print "\n$months[$mon -1] $yr\n\n"; print "Su Mo Tu We Th Fr Sa\n"; foreach (@month) { print map { $_ ? sprintf "%2d ", $_ : ' ' } @$_; print "\n"; }

Replies are listed 'Best First'.
Re: Calendar question
by graff (Chancellor) on Jun 14, 2009 at 14:13 UTC
    The OP code prints a nice, simple calendar based on an array of arrays returned by Calendar::Simple->calendar(). There's one top-level array element for each week in the month, and each one holds a reference to an array of date numbers in that week (or undef for weekdays before the first and after the last day of the month).

    If your question is "how do I flatten the AoA into a single array and convert the undef elements to empty strings?", then just loop through the "week" elements, pushing the days from each week onto a single array; here's a way to do that using "for" and "map":

    use strict; use Calendar::Simple; use Data::Dumper qw/Dumper/; my ( $mon, $yr ) = ( localtime )[4,5]; $mon++; $yr+=1900; my @weeks = calendar( $mon, $yr ); my @month; for my $week ( @weeks ) { push @month, map { $_ || "" } @$week; } print Dumper( \@month );
    Having done that, I have to wonder... Why would you want to do that?

    (updated to include a link to the module's CPAN page)

Re: Calendar question
by Anonymous Monk on Jun 14, 2009 at 13:07 UTC
    Why? @month already contains that information.
      In that case simple copy
      my(@monthar)=@month;