develooper Front page | perl.beginners | Postings from October 2009

Re: printf and zero padding

Thread Previous
Harry Putnam
October 24, 2009 18:21
Re: printf and zero padding
Message ID:
Jim Gibson <> writes:

> At 4:02 PM -0500 10/24/09, Harry Putnam wrote:
>>With this little script, how would I manage to get the shorter
>>timestamps zero padded using printf?  I now how to get padded numbers
>>but not when I'm pushing off the right margin too.
>>   #!/usr/local/bin/perl
>>   use strict;
>>   use warnings;
>>   while (my $file = shift @ARGV){
>>     my @stat = stat $file;
>>     printf "%11d %s\n",$stat[9],  $file;
>>   }
> I am not clear on what you are asking. You would use the format
> conversion '%011d' to zero-pad a number to fill 11 characters, but you
> say you know that already. Can you give an example of your desired
> output?

Egad... I really intended to include a few lines of ouput.. I didn't
even notice I hadn't... sorry.

As it is I get this: `ls`

  994039516 mms.perl

You see some of the files have been modified long enough ago that the
epochal time is enough earlier to be one digit shorter.

I wanted to see:

 0994039516 mms.perl

And in the course of explaining it, as happens to me pretty often.. I
see my mistake... Somehow I'd gotten it into my head that since it
was 1 digit short I needed to pad 1 zero...
Well... thats' true.. but it needs to come at the start of an 11
character parking place.... so anyway, thanks for making me see
the error.

I seem to forget about 90% of what I know between scripts.

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