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

How to zero fill a variable?

Thread Next
Tim Davis
March 6, 2002 07:07
How to zero fill a variable?
Message ID:
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,


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About