On Thu, Jan 31, 2002 at 05:41:15PM -0800, Pradeep Sethi wrote:
> Thanks but I am looking of any regexp substitution.
> 
> sorry for typo : I need to change 9/9/1973 to 09/09/1973

Some of the solutions posted are almost straight out of my Ineffective
Perl Programming talk. *sigh*

Assuming its coming from localtime:

    my($month, $day, $year) = (localtime)[3,4,5];
    sprintf "%02d/%02d/%04d", $month + 1, $day, 1900 + $year;  # [1]

If you literally want to change formats:

    my $date = '9/9/1973';
    my($month, $day, $year) = split '/', $date;
    $date = sprintf "%02d/%02d/%d", $month, $day, $year;


sprintf is one of the most underused functions in Perl, probably
because we don't use it for the traditional stuff C does.  In Perl is
just a really helpful formatting command.  If you ever find yourself
doing:

    $foo < 10 ? "0$foo" : $foo;
    $bar < 10 ? "0$bar" : $bar;
    print "Some stuff and $foo - $bar";

please please please do this instead:

    printf "Some stuff and %02d - %02d", $foo, $bar;

Not only is it shorter and visually clearer, but it will save me money
on airfare.


[1] I'm American.  We do everything backwards, including our date
formats.  Anyone that objects will be reminded which side of the road
you drive on.

-- 

Michael G. Schwern   <[EMAIL PROTECTED]>    http://www.pobox.com/~schwern/
Perl Quality Assurance      <[EMAIL PROTECTED]>         Kwalitee Is Job One
Don't step on my funk

Reply via email to