Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Faster and more flexible with perl' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Faster and more flexible with perl
Authored by: bharris on Aug 19, '08 04:56:49AM
The 3 column version looks great, but seems to have to run a lot of stuff. Try this one, all perl -- runs about twice as fast:
#! /usr/bin/perl -w
# multi-column calendar

# setup
$nm		= 3;	# number of months to output
$ns		= 2;	# number of spaces between months

################################################################################

#initialize
$fw		= $ns + 20;		# month field width
@c		= ();			# $c[month index][line]

(undef,undef,undef,$mday,$mon,$year) = localtime;
$mday+=0;
$mon++;
$year+=1900;


$tm = $mon;		# this month
$ty = $year;	# and year

# count back
$i = int(($nm - 1) / 2);
while ($i--) {
	if (--$tm==0) { $tm = 12; $ty--; }
}

# get calendars
foreach $i (1..$nm) {

	$_ = `cal $tm $ty`;
	s/^|$/ /gm;
	if ($tm == $mon) { s/ ( ?$mday) /[$1]/ }
	
	push @c, [ split /n/ ];
	
	if (++$tm==13) { $tm = 1; $ty++; }

}
	
# print calendars
foreach $l (0..7) {

	foreach (0..$nm-1) {
		printf "%-${fw}s", $c[$_][$l] || "";
	}
	
	print "n";
}


exit(0);


[ Reply to This | # ]