in reply to Re^2: Using exists to check element in array
in thread Using exists to check element in array
and he asked for the rational.
Hasn't that been answered already?
It's because exists $a[i] doesn't really check if the element exists in the array. It's not sufficient for the element to be part of the array; the pointer in the array buffer must be something other than NULL.
For example,
my @a; $a[1] = undef; $a[2] = 0; $a[3] = 7;
Element of the array | exists | defined | True | |
---|---|---|---|---|
$a[0] | Yes | No | No | No |
$a[1] | Yes | Yes | No | No |
$a[2] | Yes | Yes | Yes | No |
$a[3] | Yes | Yes | Yes | Yes |
$a[4] | No | No | No | No |
It might also be warning of problems with using NULL as a special value. I suspect Perl is quite consistent at keeping NULL elements NULL, but there might be surprise cases (for (@a)? for ((), @a)? f(@a)?)
Update: Replaced some uses of "exists".
|
---|