Contributed by xorl on Oct 15, 2001 at 22:46 UTC
Q&A  > arrays


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.


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


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

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!
  • 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, 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
  • 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.