<?xml version="1.0" encoding="windows-1252"?>
<node id="180998" title="Answer: Like in hashes, do we have something like exists in arrays as well." created="2002-07-11 07:27:34" updated="2005-08-14 02:20:01">
<type id="1888">
categorized answer</type>
<author id="11732">
QandAEditors</author>
<data>
<field name="doctext">
&lt;code&gt;
$ perldoc -f exists
       exists EXPR
               Given an expression that specifies a hash element
               or array element, returns true if the specified
               element in the hash or array has ever been ini-
               tialized, even if the corresponding value is unde-
               fined.  The element is not autovivified if it
               doesn't exist.

                   print "Exists\n"    if exists $hash{$key};
                   print "Defined\n"   if defined $hash{$key};
                   print "True\n"      if $hash{$key};

                   print "Exists\n"    if exists $array[$index];
                   print "Defined\n"   if defined $array[$index];
                   print "True\n"      if $array[$index];

               A hash or array element can be true only if it's
               defined, and defined if it exists, but the reverse
               doesn't necessarily hold true.

               Given an expression that specifies the name of a
               subroutine, returns true if the specified subrou-
               tine has ever been declared, even if it is unde-
               fined.  Mentioning a subroutine name for exists or
               defined does not count as declaring it.  Note that
               a subroutine which does not exist may still be
               callable: its package may have an "AUTOLOAD"
               method that makes it spring into existence the
               first time that it is called -- see the perlsub
               manpage.

                   print "Exists\n"    if exists &amp;subroutine;
                   print "Defined\n"   if defined &amp;subroutine;

               Note that the EXPR can be arbitrarily complicated
               as long as the final operation is a hash or array
               key lookup or subroutine name:

                   if (exists $ref-&gt;{A}-&gt;{B}-&gt;{$key})  { }
                   if (exists $hash{A}{B}{$key})       { }

                   if (exists $ref-&gt;{A}-&gt;{B}-&gt;[$ix])   { }
                   if (exists $hash{A}{B}[$ix])        { }

                   if (exists &amp;{$ref-&gt;{A}{B}{$key}})   { }

               Although the deepest nested array or hash will not
               spring into existence just because its existence
               was tested, any intervening ones will.  Thus
               "$ref-&gt;{"A"}" and "$ref-&gt;{"A"}-&gt;{"B"}" will spring
               into existence due to the existence test for the
               $key element above.  This happens anywhere the
               arrow operator is used, including even:

                   undef $ref;
                   if (exists $ref-&gt;{"Some key"})      { }
                   print $ref;             # prints HASH(0x80d3d5c)

               This surprising autovivification in what does not
               at first--or even second--glance appear to be an
               lvalue context may be fixed in a future release.

               See the Pseudo-hashes: Using an array as a hash
               entry in the perlref manpage for specifics on how
               exists() acts when used on a pseudo-hash.

               Use of a subroutine call, rather than a subroutine
               name, as an argument to exists() is an error.

                   exists &amp;sub;        # OK
                   exists &amp;sub();      # Error
&lt;/code&gt;
</field>
<field name="parent_node">
180997</field>
</data>
</node>
