Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Like in hashes, do we have something like exists in arrays as well.

( #180997=categorized question: print w/ replies, xml ) Need Help??
Contributed by rmalhotra on Jul 11, 2002 at 11:11 UTC
Q&A  > arrays


Answer: Like in hashes, do we have something like exists in arrays as well.
contributed by Abigail-II

$ 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 &subroutine; print "Defined\n" if defined &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->{A}->{B}->{$key}) { } if (exists $hash{A}{B}{$key}) { } if (exists $ref->{A}->{B}->[$ix]) { } if (exists $hash{A}{B}[$ix]) { } if (exists &{$ref->{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->{"A"}" and "$ref->{"A"}->{"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->{"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 ⊂ # OK exists &sub(); # Error
Answer: Like in hashes, do we have something like exists in arrays as well.
contributed by SuicideJunkie

use strict; use warnings; my @stuff = (0,1, undef); for (0..3) { print "Element $_ : " . (!exists ($stuff[$_]) ? 'Does not exist' : !defined ($stuff[$_]) ? 'is Undef' : "is '$stuff[$_]'") . "\n"; }
Gives:
>perl test.pl Element 0 : is '0' Element 1 : is '1' Element 2 : is Undef Element 3 : Does not exist
Answer: Like in hashes, do we have something like exists in arrays as well.
contributed by Rahul6990

This will work for you:

print "$element in @array1\n" if(grep( /$element/, @array1 )) ;

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (8)
    As of 2014-12-25 10:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (160 votes), past polls