grep basically returns all true values from the array, and 0 is interpolated as false.
For example, grepused here to filter true values in @b,
my @dirty = (0,1,2,3,,0,5,);
my @clean = grep {$_} @dirty;
for(@clean) {print "$_\n";}
Returns
1
2
3
5
A little trick to keep zeros involves adding a new line to the grep argument:
my @dirty = (0,1,2,3,,0,5,);
my @clean = grep {"$_\n"} @dirty;
for(@clean) {print "$_\n";}
What returns
0
1
2
3
0
5
source
share