@n = grep $_, @n;
This will put into @n all elements of the original @n that evaluate to true. But that means that elements whos value is 0, or "", or anything else that evaluates to false, will get skipped over. "undef" does evalute to false, by the way.
@n = grep defined, @n
This is what you want if all you care about is getting rid of elements whos value is undef. This will retain elements of zero value or empty strings, so long as they're not undef. I worked up several test snippets showing that this works for that type of situation. No need to show them here; it's pretty basic stuff.
@n = grep defined and length, @n
Here you're getting into trouble because of the low precedence of the "and" logical short circuit operator. You would be better served by using "&&" in this case. "and" binds at such a low precedence that everything to its right is no longer seen as part of the parameter list to grep. Wierd, huh? "&&" solves this problem (parenthesis placed in the right place would solve it too). But then you're going to be saying "if its value is defined and of a length greater than zero". That may be ok, or it might be better to say "@n = grep defined && $_, @n;". That way instead of testing for length, you're testing for truth.
And I won't get into the rest of your solutions because they are too unPerlish to wade through. I think that in general you'll find it to be a good way to run amok; to try to delete array elements while iterating through the array with a C-style loop.
Dave
In reply to Re: Removing null values from within an array
by davido
in thread Removing null values from within an array
by coldfingertips
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |