in reply to can't remove all zeroes from an array
The problem you encountered with splice() is very common: Splicing elements in or out of a list upsets the numbering of the remaining elements. Often the solution is to traverse the list in reverse order, beginning at the high end. Thus, splice can be used much in the way you originally had it:
or, more compact:my $i = $#arry; while ( $i >= 0 ) { if ($arry[$i] == 0) {splice @arry, $i,1;} $i-- ; }
Anno$arry[ $_] or splice @arry, $_, 1 for reverse 0 .. $#arry;
|
|---|