masto.es es uno de los varios servidores independientes de Mastodon que puedes usar para participar en el fediverso.
Bienvenidos a masto.es, el mayor servidor de Mastodon para hispanohablantes de temática general. Registros limitados actualmente a invitaciones.

Administrado por:

Estadísticas del servidor:

2 K
usuarios activos

@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

PerlCriticPerl::CriticInstantly find bugs in your Perl code

@mjg @ology

Thanks a lot for taking the time to show me this alternative. I appreciate. I'll update the post and credit you. Thanks!