in reply to A quick date swap from a string
Note: if you omit the \n in the die "text", Perl will add the module and line number of the death to the output.sub num_month { my $date_line = shift; #like jan 12 2009 or january 12 2009 my %months = (JAN => '01', FEB => '02', MAR => '03', APR => '04', MAY=> '05', JUN => '06', JUL => '07', AUG => '08', SEP => '09', OCT => '10', NOV => '11', DEC => '12', JANUARY => '01, ....etc.....); my (@tokens) = split (/\s+/, uc($date_line)); foreach my $token (@tokens) { return ($months{$token} if $months{$token}); } die "no month in @tokens"; ## optional but you may want this }
Update:
showing how slit on \s and , can be done and how to get say the 2nd thing in a split via a list slice without using foreach():
#!/usr/bin/perl -w use strict; my $date_line = "Jan 2 , 2009 \n"; my (@tokens) = split (/[\s,]+/, uc($date_line)); print "@tokens\n"; #prints JAN 2 2009 my $date_line2 = "2 jan , 2009 \n"; #to get 2nd thing in split, use a list slice... my $month_text = (split (/[\s,]+/, uc($date_line2)))[1]; print "$month_text\n"; #prints JAN
|
|---|