in reply to Empty or not empty ?
$#array returns the index of the last element in @array (not the number of elements in @array).
If @array has 2 elements, $#array returns 1* (because the elements are numbered 0 and 1*).
If @array has 1 element, $#array returns 0*.
If @array has no elements, $#array returns -1*.
In scalar context, @array returns the number of elements in @array.
If @array has 2 elements, @array returns 2.
If @array has 1 element, @array returns 1.
If @array has no elements, @array returns 0.
So use
if ($#array == -1), or
if (@array == 0), or
if (!@array)
* — That's assuming $[ has been left unchanged. Changing $[ is highly discouraged.
|
|---|