develooper Front page | perl.beginners | Postings from March 2002

How to zero fill a variable?

Thread Next
From:
Tim Davis
Date:
March 6, 2002 07:07
Subject:
How to zero fill a variable?
Message ID:
B99093684C73A440B085E162BB8C7E0A773328@meiboiexsrv02.mei.hst
Fellow Perl lovers,

I'm trying to zero fill a numeric variable (which in this case happens to be the current month and day).

The sprintf (%02d) function would seem a likely candidate for this, but I don't actually want to print the value.
It's being used as part of a filename.

I've looked at the pack function, but haven't been able to decipher from the documentation a template that would work.

Since in this case I only have to zero fill at most one position, here's the lengthy 'brute force' method that I came up with:

....
my $month = length((localtime)[4] + 1) == 1 ? '0' . ((localtime)[4] + 1) : (localtime)[4] + 1;
my $day = length((localtime)[3]) != 1 ? (localtime)[3] : '0' . (localtime)[3];
my $monthday = $month . $day;
my $output_file = join ('_', 'resend' , $monthday , "$key");
....

Is there a more elegant way of doing this?

Thanks for your help,

Tim


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About