Incremental numbers in PerlScript
am 07.12.2007 23:30:50 von Peter FrankHi,
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
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
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
Peter Frank
> 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.