Recently I saw a pretty postcard in a shop which had a list of dates on it as follows:
And it seemed a nice idea to send such a message by e-mail to my loved one.I love you on the 1st of January 2011 I love you on the 2nd of January 2011 I love you on the 3rd of January 2011 ... I love you on the 31st of December 2011
Now I am a terrible typist and I am sure I will type many errors in such e-mail, so why not writing a script that does it for me?
Here is what I wrote:
Usage is perl everyday.pl start_date end_date Dates are in the ddmmyyyy, dd-mm-yyyy or dd/mm/yyyy format. If you leave out end_date it will default to one year after start_date. If you also leave out start_date, it will default to today.# every day I love you use Modern::Perl; use Date::Calc::Iterator; use Lingua::EN::Inflect qw/ORD/; my @months = qw /January February March April May June July August September Octo +ber November December/; my $start = $ARGV[0] ? parse_date( $ARGV[0] ) : [ (localtime)[5] + 1900, (localt +ime)[4] + 1, (localtime)[3] ]; my $end = $ARGV[1] ? parse_date( $ARGV[1] ) : [ $start->[0] + 1, $star +t->[1], $start->[2] ]; my $i = Date::Calc::Iterator->new( from => $start, to => $end ); my @result; my $day; push @result, join ' ', 'I love you on the', ORD( $day->[2] ), 'of', $months[ $day->[1] - 1 +], "$day->[0]." while $day = $i->next; pop @result unless $ARGV[1]; # drop the last one if the end is calc +ulated by default say for @result; sub parse_date { my $date = shift; return undef unless $date; if ( $date =~ m/^(\d{2})[\/-]?(\d{2})[\/-]?(\d{4})$/ ) { return [ $3, $2, $1 + 0 ]; } else { return undef; } }
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: "I love you every day"-script
by syphilis (Archbishop) on Sep 25, 2011 at 14:23 UTC | |
|
Re: "I love you every day"-script
by spx2 (Deacon) on Nov 25, 2011 at 13:30 UTC | |
by CountZero (Bishop) on Nov 25, 2011 at 16:44 UTC |