If you want to remove items, the best tool would be grep :
@array = grep { !/O/ } @array;
Technically, it is possible to do this with a for loop, but you will have to jump over some hoops to do this, or copy to another array:
my @result; for (@array) { if (/O/) { push @result, $_; } }
You should know that for and foreach are aliases, and they do the same. This is the C-style syntax you are thinking of:
for (@array) { print $_, "\n"; }
source share