@RevistaOccamsRazor This is very old-skool perl.
@ology Yes, but I believe this is the most useful one... And I like old-skool ;)
@RevistaOccamsRazor #Perl can loop directly over arrays and lists. Plus, since you have only one operation in that first loop, you can use a postfix for
. So you could have written:
$a[$_] = $_ + 10 for 0 .. 9;
Or if you prefer a more traditional for
loop:
for $i (0 .. 9) {
$a[$i] = $i + 10;
}
Mind you, you’re just transforming a list into an another list/array, so map
is even more idiomatic:
@a = map {$_ + 10} 0 .. 9;
The basic principle is that C-style for (;;)
loops are rarely needed, unless you’re iterating more than one step at a time or you need some kind of side-effect on an index variable.
Perl devs often use the perlcritic
tool from the command line, and it has a specific policy module calling out C-style for loops.
/cc @ology