Padded 3 digit numeric series
12 answers - 573 bytes -

Taking the chicken way out but I can't think of the right tricky
search strings to uncover a tried and true way to output a 3 digit
padded numeric series. In this case its for file names. And needs to
roll over to 4 digit in the event there are enough files.
There is no problem of clobbering since files are being renamed as
they are moved to a new clean directory. I just can't recall how to
make my incremented counter start at 000 and go:
001.ext
002.ext
003.ext
etc.
Its just the numeric part I need a jump start on.
No.1 | | 407 bytes |
| 
Sat, 11 Mar 2006, Harry Putnam wrote:
Its just the numeric part I need a jump start on.
Have you looked at sprintf yet? `perldoc -f sprintf`
That's probably the easiest way.
Alternatively, you could do some kind of silly subroutine that padded
two zeroes if $n 10, one zero if $n 100, etc. It wouldn't be that
hard to do, but I think sprintf will be much easier.
No.2 | | 891 bytes |
| 
Harry Putnam wrote:
Taking the chicken way out but I can't think of the right tricky
search strings to uncover a tried and true way to output a 3 digit
padded numeric series. In this case its for file names. And needs to
roll over to 4 digit in the event there are enough files.
There is no problem of clobbering since files are being renamed as
they are moved to a new clean directory. I just can't recall how to
make my incremented counter start at 000 and go:
001.ext
002.ext
003.ext
etc.
Its just the numeric part I need a jump start on.
$ perl -le'
for my $number ( 0 4, 997 1003 ) {
my $filename = sprintf q[%03d.ext], $number;
print $filename;
}
'
000.ext
001.ext
002.ext
003.ext
004.ext
997.ext
998.ext
999.ext
1000.ext
1001.ext
1002.ext
1003.ext
John
No.3 | | 477 bytes |
| 
3/11/06, Chris Devers <cdevers (AT) pobox (DOT) comwrote:
Sat, 11 Mar 2006, Harry Putnam wrote:
Its just the numeric part I need a jump start on.
Have you looked at sprintf yet? `perldoc -f sprintf`
That's probably the easiest way.
Alternatively, you could do some kind of silly subroutine that padded
two zeroes if $n 10, one zero if $n 100, etc. It wouldn't be that
hard to do, but I think sprintf will be much easier.
--
No.4 | | 796 bytes |
| 
Harry Putnam wrote:
Taking the chicken way out but I can't think of the right tricky
search strings to uncover a tried and true way to output a 3 digit
padded numeric series. In this case its for file names. And needs to
roll over to 4 digit in the event there are enough files.
There is no problem of clobbering since files are being renamed as
they are moved to a new clean directory. I just can't recall how to
make my incremented counter start at 000 and go:
001.ext
002.ext
003.ext
etc.
Its just the numeric part I need a jump start on.
Believe it or not, write the number as a string.
#!/usr/bin/perl
use strict;
use warnings;
for ( '000' '100' ){
print "$_\n";
}
__END__
No.5 | | 845 bytes |
| 
Robin Sheat wrote:
Sunday 12 March 2006 18:22, Shawn Corey wrote:
>Believe it or not, write the number as a string.
>for ( '000' '100' ){
>print "$_\n";
>}
Note that if you have a string such as "000", you can treat it like a
number:
my $a = '000';
$a++;
print "$a\n";
This will print '001'.
To have it go to 4 digits, well, that's as easy as you might expect:
my $a = '999';
$a++;
print "$a\n";
This will print '1000'.
This trick also works on letters:
#!/usr/bin/perl
use strict;
use warnings;
for ( 'a0' 'z9' ){
print "$_\n";
}
__END__
See `perldoc perlop` and search for "Auto-increment and Auto-decrement"
No.6 | | 459 bytes |
| 
"Chas " <chas.owens (AT) gmail (DOT) comwrites:
sprintf is definitly the correct answer, but just to prove TIMTWTDI
#!/usr/bin/perl
use strict;
print zeropad(5,100), "\n";
sub zeropad {
my ($count, $n) = @_;
return substr '0' x $count . $n, -$count;
}
I probably wasn't clear enough in my request
This ouputs:
./test.pl
00100
I'm after:
001 thru 999 and then 1000
No.7 | | 280 bytes |
| 
Robin Sheat <robin (AT) kallisti (DOT) net.nzwrites:
Note that if you have a string such as "000", you can treat it like a
number:
my $a = '000';
$a++;
print "$a\n";
Haaa, there it the obvious way I didn't see in Shawns post.
thanks.
No.8 | | 432 bytes |
| 
"John W. Krahn" <krahnj (AT) telus (DOT) netwrites:
>Its just the numeric part I need a jump start on.
>
$ perl -le'
for my $number ( 0 4, 997 1003 ) {
my $filename = sprintf q[%03d.ext], $number;
print $filename;
}
'
Ahh. Nice, thanks. I didn't recognize the
`q[]' usage but it appears to operate the same as
"%03d,ext", $number;
No.9 | | 414 bytes |
| 
Shawn Corey <shawnhcorey (AT) magma (DOT) cawrites:
[]
Believe it or not, write the number as a string.
#!/usr/bin/perl
use strict;
use warnings;
for ( '000' '100' ){
print "$_\n";
}
__END__
Thanks
This has the right output but its not very obvious how one would use
this to increment a counter and files are being renamed.
No.10 | | 342 bytes |
| 
Harry Putnam wrote:
Ahh. Nice, thanks. I didn't recognize the
`q[]' usage but it appears to operate the same as
"%03d,ext", $number;
No, it would be the same as '%03d.ext' the other hand, qq[%03d.ext]
would be the same as "%03d.ext"
See `perldoc perlop` and search for "Quote and Quote-like "
No.11 | | 499 bytes |
| 
Harry Putnam wrote:
This has the right output but its not very obvious how one would use
this to increment a counter and files are being renamed.
As you would any other variable that contains a string.
my $file = "$_.ext";
or
my $file = $_ . '.ext';
or
my $file = sprintf( '%s.ext', $_ );
or even
my $file = sprintf( q/%03d.ext/, $_ );
though this sort of defeats the purpose of auto-incrementing a string.
No.12 | | 364 bytes |
| 
Robin Sheat <robin (AT) kallisti (DOT) net.nzwrites:
Note that if you have a string such as "000", you can treat it like a
number:
my $a = '000';
$a++;
print "$a\n";
This will print '001'.
That looks like the tidiest way to do it in this case.
Just start the count with my $a and were done.
Thanks