Incremental numbers in PerlScript

Incremental numbers in PerlScript

am 07.12.2007 23:30:50 von Peter Frank

Hi,

Can someone please tell me how I can get incremental numbers by using
variables in PerlScript, i.e the result should be for example 01, 02,
03, etc.?

Peter

Re: Incremental numbers in PerlScript

am 07.12.2007 23:41:25 von jurgenex

Peter Frank wrote:
> Can someone please tell me how I can get incremental numbers by using
> variables in PerlScript, i.e the result should be for example 01, 02,
> 03, etc.?

Not sure where your problem is. Many ways, e.g. even a trivial "(1..3)".
If you want to print them in a specific format like e.g. with a leading 0
then check out printf().

jue

Re: Incremental numbers in PerlScript

am 07.12.2007 23:52:36 von darkon

Peter Frank wrote:

> Can someone please tell me how I can get incremental numbers by
> using variables in PerlScript, i.e the result should be for
> example 01, 02, 03, etc.?

Here's one way:

for my $n (0 .. 99) {
my $formatted_n = sprintf "%02d", $n;
print $formatted_n, "\n";
}


See the documentation for sprintf for more info. If you just want to
print out the values you might use printf. I used sprintf in case
you wanted to use them internally, say perhaps as part of a hash key.