in reply to Re^4: Using exists to check element in array
in thread Using exists to check element in array
Your table shows exactly a consistent behavior to exists when used with hashes.
No, it's not consistent with hashes.
$ perl -e' my %h = ( a => 4, c => 6 ); printf( "exists %s return false for elements found in a hash.\n", ( grep { !exists( $h{$_} ) } keys %h ) ? "can" : "didn\x27t" ); my @a; $a[0] = 4; $a[2] = 6; printf( "exists %s return false for elements found in an array.\n", ( grep { !exists( $a[$_] ) } keys @a ) ? "can" : "didn\x27t" ); ' exists didn't return false for elements found in a hash. exists can return false for elements found in an array.
The very existence of this inconsistency is reason enough for a warning.
$a[0] was never set, hence it doesn't exist.
No, that means it wasn't set. Setting or not setting has nothing to do whether an element is found in an array or not, and with what exists returns.
exists can obviously return false for elements that were never set, but it can also return true for such elements as the following demonstrates:
$ perl -Mv5.14 -e'my @a; \$a[0]; say exists( $a[0] ) || 0;' 1
Your implication that exists checks whether an element was set or not is incorrect. As I already mentioned, exists checks if an element is within the array and if it isn't NULL.
I don't understand, probably above my expertise.
These are things that might unintentionally change replace the NULL with a (pointer to a) scalar. I didn't test.
Upd: I just did and they don't. But \$a[0] does, as shown above.
|
|---|