Perl sequence of numbers with leading zeros

Viewed 3317

Is there some easy way (without sprintf and, of course, printf) to get a list of (001, 002, ... 100) in Perl? In bash it was something like seq -w 1 100. What about Perl?

2 Answers

you mean like this?

for ('001'..'100') {
    print "$_\n";
}

.. in a range, returns a list of values counting by ones, from the left value to the right value.

For more details about how to use range, please refer to: Perldoc range operator and this link

Printf was created for problems like this. Using it will help you get the answers you want faster.

foreach my $number ( 1 .. 100 ) {
     printf "%03d\n", $number;
}

The % is the "begin a format sequence" The 0 is "leading zeros" The 3 is "three digits minimum" The d is "treat the parameter as digits (integer)"

Related