It's the same question as this one but using Perl !
I would like to iterate over a value with just one leading zero
The equivalent in shell would be :
for i in $(seq -w 01 99) ; do echo $i ; done
Thanks Community !
It's the same question as this one but using Perl !
I would like to iterate over a value with just one leading zero
The equivalent in shell would be :
for i in $(seq -w 01 99) ; do echo $i ; done
Thanks Community !
I would consider to use sprinft to format $i according to your requirements. E.g. printf '<%06s>', 12; prints <000012>.
Check Perl doc about sprinft in case you are unsure.
Try something like this:
foreach (1 .. 99) {
$s = sprintf("%02d",$_);
print "$s\n";
}
The .. is called the Range Operator and can do different things depending on its context. We're using it here in a list context so it counts up by ones from the left value to the right value. So here's a simpler example of it being used; this code:
@list = 1 .. 10;
print "@list";
has this output:
1 2 3 4 5 6 7 8 9 10
The sprintf function allows us to format output. The format string %02d is broken down as follows:
% - start of the format string0 - use leading zeroes2 - at least two characters wided - format value as a signed integer.So %02d is what turns 2 into 02.
Since the leading zero is significant, presumably you want to use these as strings, not numbers. In that case, there is a different solution that does not involve sprintf:
for my $i ("00" .. "99") {
print "$i\n";
}
Well, if we're golfing, why not:
say for "01".."99"`
(assuming you're using 5.10 and have done a use 5.010 at the top of your program, of course.)
And if you do it straight from the shell, it'd be:
perl -E "say for '01'..'99'"