loopy.txt
# for() in perl
use feature qw/say/; # instead of print
use strict;
use warnings;
my @list = qw/a b c/;
#===================================================
# this is probably not what you really want:
for(my $i = 0; $i < scalar @list; $i++) {
say $list[$i];
}
for(my $i = 0; $i <= $#list; $i++) {
say $list[$i];
}
for(my $i = 0; $i < @list; $i++) {
say $list[$i];
}
#===================================================
# this is a tad better:
for my $i (0..@list-1) {
say $list[$i];
}
for my $i (0..$#list) {
say $list[$i];
}
#===================================================
# this is more like it:
# remember that $item is an alias for the element in
# the list, so if you change $item inside the loop,
# the element in the list will also be updated
for my $item (@list) {
$item .= 'foo'; # will add 'foo' to all elements in @list
say $item;
}
#===================================================
# maybe this is really what you want, if you are
# required to know the index:
my $i = 0;
while($i < @list) {
if($list[$i] eq 'afoo') {
splice @list, $i, 1; # remove one item
}
else {
$i++;
}
}