in reply to can't remove all zeroes from an array

GrandFather has pointed out the best solution to your problem using grep and the reason why your code was failing. However, it is sometimes a useful technique, when doing something that might invalidate an index, to consider working back from the end of the array to the beginning. That way you can avoid mucking the index up.

use strict; use warnings; my @arr = (9, 0, 0, 5, 3, 0, 0, 0, 2, 0, 1, 0); print qq{@arr\n}; for my $idx ( reverse 0 .. $#arr ) { splice @arr, $idx, 1 unless $arr[$idx]; } print qq{@arr\n};

This produces

9 0 0 5 3 0 0 0 2 0 1 0 9 5 3 2 1

I hope this is useful.

Cheers,

JohnGG