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.


In reply to Re^5: Using exists to check element in array by ikegami
in thread Using exists to check element in array by david

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.