Andrej Kastrin wrote:
Hi all,

Suppose that we have numbers 1 to 1000 and we want all numbers be equal length; e.g.:
0001
0002
0003
...
..
1000

Any idea on how to fix this problem?

Best, Andrej



# perldoc -f sprintf

for ( 1 .. 1000 ){
  printf "%04d\n", $_;
}


# version 2
#!/usr/bin/perl

use strict;
use warnings;

use POSIX;

my $max = shift @ARGV;
my $len = 1;
for ( my $n = 1; $n <= $max; $n *= 10 ){
  $len ++;
}
$len --;

for ( 1 .. $max ){
  printf "%0*d\n", $len, $_;
}

__END__


--

Just my 0.00000002 million dollars worth,
   --- Shawn

"Probability is now one. Any problems that are left are your own."
   SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to