in reply to How to determine if an array contains an undef value?
An array containing an undef is not empty; it contains the undef. In that case @array in a scalar context (eg. if( @array ); returns the number of elements, which is 1, therefore true.
It may be an undefined value, but it is still a value.
So, based upon what you've said, your problem becomes not: Is the array empty?; but rather: Does the array contain any defined values?
Whilst that can be determined with grep, and may be perfectly adequate if your arrays are known to be small; if there is any chance that you could be dealing with large arrays that might contain lots of undefs, then you might also consider using one of the any() implementations (eg. List::Util::any() ) which will short circuit when it sees the first defined value:
print "\@b is not Empty any\n" if any { defined() } @b;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to determine if an array contains an undef value?
by thanos1983 (Parson) on Jan 04, 2015 at 22:28 UTC |