I probably would have converted the start/end month/day to GMT values and then iterated over them. Something like this:
#!/usr/bin/perl use warnings; use strict; use POSIX qw(strftime); use Time::Local; my @breakfast = qw/bagel cereal toast yogurt/; my @lunch = qw/sandwich milkshake pb&j hoagie/; my @dinner = qw/pasta rice chicken steak/; print "Enter start month (ie., for January, enter 1): "; chomp(my $start_mon = <STDIN>); print "Enter start day (ie., 20): "; chomp(my $start_day = <STDIN>); print "Enter end month (ie., for March, enter a 3): "; chomp(my $end_mon = <STDIN>); print "Enter end day (ie., 21): "; chomp(my $end_day = <STDIN>); print "Save to file [hit enter for default 'plan']: "; chomp(my $file = <STDIN>); $file ||= "plan"; my $start_gmt = timegm(0,0,0,$start_day,$start_mon-1,0); my $end_gmt = timegm(0,0,0,$end_day,$end_mon-1,0); open(FH, ">$file") or die "open: $!\n"; for (my $g = $start_gmt; $g <= $end_gmt; $g += 86400) { print FH my $d = strftime("%b %e", gmtime($g)), "\n"; print "-" x length($d), "\n"; print FH "Breakfast: $breakfast[rand @breakfast]\n"; print FH "Lunch : $lunch[rand @lunch]\n"; print FH "Dinner : $dinner[rand @dinner]\n"; print "-" x length($d), "\n"; } print "Written to file $file.\n"; close(FH);
There are all sorts of modules on CPAN dealing with dates and times that could have helped you out too.
In reply to Re: Critique
by duff
in thread Critique
by phenom
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |