in reply to can't remove all zeroes from an array
Praveen, ikegami, Grandfather, etc all had wonderful ideas.
I'd use grep, but my code would probably look like this:
@array = (1, 0, 2, 0, 3, -1, 0, 4, 0, -5, 0, 123); @nonzeros = grep {$_} @array;
or
@array = (1, 0, 2, 0, 3, -1, 0, 4, 0, -5, 0, 123); @nonzeros = grep {$_ != 0} @array;
Both are based on the assumption that the array's contents are strictly numeric; if you want to remove strings, your grep would require a different regex.
|
|---|