Perl: remove empty elements from array
I have an array with a bunch of empty strings, and other "empty" items, that I want to remove. Perl's grep() command makes this very simple:
@a = ("one", "", "three", "four", 0, "", undef, "eight");
@b = grep($_, @a);
# @b = ("one","three","four","eight");
The first argument to grep is an expression which evaluates whether $_
is a truthy value. This could easily also have been $_ ne ""
so we don't also filter out ""
and 0
.