Equal length numbers

2005-12-15 Thread Andrej Kastrin

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


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




RE: Equal length numbers

2005-12-15 Thread RICHARD FERNANDEZ
 -Original Message-
From: Andrej Kastrin [mailto:[EMAIL PROTECTED] 
Sent: Thursday, December 15, 2005 9:09 AM
To: beginners@perl.org
Subject: Equal length numbers

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

HTH

richf

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




Re: Equal length numbers

2005-12-15 Thread Shawn Corey

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.0002 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




Re: Equal length numbers

2005-12-15 Thread Dr.Ruud
Andrej Kastrin schreef:

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

perl -e $\=qq(\n); print for (q(0001)..q(1000))

-- 
Affijn, Ruud

Gewoon is een tijger.


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