Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Is there an isArray() in Perl?

by xorl (Deacon)
on Oct 15, 2001 at 22:46 UTC ( [id://118961]=perlquestion: print w/replies, xml ) Need Help??

xorl has asked for the wisdom of the Perl Monks concerning the following question: (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.


Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: Is there an isArray() in Perl?
by Flame (Deacon) on Oct 16, 2001 at 00:33 UTC
    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
Re: Is there an isArray() in Perl?
by suaveant (Parson) on Oct 15, 2001 at 22:48 UTC
    look at the ref() function in perl, it tells you the type of ref you have.
Re: Is there an isArray() in Perl?
by jacques (Priest) on May 10, 2005 at 20:22 UTC
    Alternatively, you can use
    use UNIVERSAL 'isa'; foreach $item (@array) { if (isa($item, 'ARRAY')) { print "We've got an array!!\n"; } }
      UNIVERSAL is available even without use'ing it, so the previous code snippet can be rewritten as:
      foreach $item (@array) { if (UNIVERSAL::isa($item, 'ARRAY')) { print "We've got an array!!\n"; } }
        Correct; the only point of use'ing UNIVERSAL is to import the function(s)...which he does.
Re: Is there an isArray() in Perl?
by jdporter (Paladin) on Mar 03, 2008 at 15:49 UTC
    The reftype function in module Scalar::Util is good. It returns 'ARRAY' for any array reference, even if it's blessed.

        Right. That makes it inappropriate as an isArray function, since it returns false for some arrays.

      If you work with normal array variables (not a reference ones), then you will never need such function. The sigil @ tells your right away that it's an array.

      Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: Is there an isArray() in Perl?
by Molt (Chaplain) on May 11, 2005 at 11:37 UTC

    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; } }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://118961]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2024-07-17 14:20 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.