Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Is there an isArray() in Perl?

( #118961=categorized question: print w/ replies, xml ) Need Help??
Contributed by xorl on Oct 15, 2001 at 22:46 UTC
Q&A  > arrays


Description:

I need to figure out if an element in an array is an array. I have something like:
my @array=("red", "blue", "green", ["purple", "orange"]);
I want to test each element and if the element is array I want it to do something special.

Thanks

Answer: Is there an isArray() in Perl?
contributed by Flame

Try:

foreach $item (@array){ if(ref($item) eq 'ARRAY'){ #It's an array reference... #you can read it with $item->[1] #or dereference it uisng @newarray = @{$item} }else{ #not an array in any way... } }
Hope this helps
Answer: Is there an isArray() in Perl?
contributed by suaveant

look at the ref() function in perl, it tells you the type of ref you have.

Answer: Is there an isArray() in Perl?
contributed by jacques

Alternatively, you can use universal.pm

use UNIVERSAL 'isa'; foreach $item (@array) { if (isa($item, 'ARRAY')) { print "We've got an array!!\n"; } }
Answer: Is there an isArray() in Perl?
contributed by jdporter

The reftype function in module Scalar::Util is good. It returns 'ARRAY' for any array reference, even if it's blessed.

Answer: Is there an isArray() in Perl?
contributed by Molt

If there's a chance of wanting to catch bless'ed arrays, or dealing with psychologically nasty code, such as my $item=bless {}, 'ARRAY', then it's best to be paranoid and check if you can actually treat the item as an array.

The following code snippet shows a function that does this kind of check:

for my $item (@array) { print "We've got an array" if is_array($item); } sub is_array { my ($ref) = @_; # Firstly arrays need to be references, throw # out non-references early. return 0 unless ref $ref; # Now try and eval a bit of code to treat the # reference as an array. If it complains # in the 'Not an ARRAY reference' then we're # sure it's not an array, otherwise it was. eval { my $a = @$ref; }; if ($@=~/^Not an ARRAY reference/) { return 0; } elsif ($@) { die "Unexpected error in eval: $@\n"; } else { return 1; } }

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 contemplating the Monastery: (7)
    As of 2014-12-28 13:11 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

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





      Results (181 votes), past polls