This is the same question as this one , but using Perl!
I would like to iterate over a value with one single zero.
Equivalent in shell:
for i in $(seq -w 01 99) ; do echo $i ; done
Since the leading zero is significant, it is assumed that you want to use them as strings, not numbers. In this case, there is another solution that does not include sprintf:
for my $i ("00" .. "99") { print "$i\n"; }
Try something like this:
foreach (1 .. 99) { $s = sprintf("%02d",$_); print "$s\n"; }
.. is called a Range Operator and can do different things depending on its context. We use it here in the context of a list, so it counts values ββfrom the left value to the right value. So here is a simpler example of its use; this code:
..
@list = 1 .. 10; print "@list";
has this conclusion:
1 2 3 4 5 6 7 8 9 10
The sprintf function allows us to format the output. The format string %02d broken as follows:
sprintf
%02d
%
0
2
d
So %02d is what turns 2 into 02 .
02
printf("%02d\n",$_) foreach (1..20)
I would like to use sprinft to format $ i according to your requirements. For instance. printf '<%06s>', 12; prints <000012> . Check out the Perl doc about sprinft if you are unsure.
printf '<%06s>', 12;
<000012>
foreach $i (1..99) {printf "%02d\n", $i;}
Well, if we play golf, why not:
say for "01".."99"`
(assuming you use 5.10 and, of course, did use 5.010 at the top of your program.)
use 5.010
And if you do it directly from the shell, it will be:
perl -E "say for '01'..'99'"
Source: https://habr.com/ru/post/1300719/More articles:How do I know the preferred size of a JPanel that does not display according to its contents? - javaExtract gettext translations from PHP heredoc syntax? - phpIBM MQSeries problem: the remote host closes the connection when writing a message (error 10054) - c #Using Vector Data Structures - Design and Syntax Issues - c ++Get manifest file from executable JAR - javaDownload Stream and Play Video in ASP.NET - c #JQuery to display success message in asp.net web forms - jqueryHow to convert the downloaded video and get a screenshot from this file? - flashHow to control the number of running workflows? - erlangSQLDataReader: handling null values ββ- syntaxAll Articles