Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have a complex array like this:

my %a = (one => [ ['a','b'],['c','d'] ])

My question is how do I find the length of $a{one}, which is 2 in this case. I have tried scalar(@a{one}) and some other things.

scalar(@a{one}) gives ARRAY(0x805e654)

Thanks, Dave

Replies are listed 'Best First'.
Re: Array length
by dragonchild (Archbishop) on Oct 15, 2004 at 04:25 UTC
    @{$a{one}}. $a{one} is a reference to an array, not an array itself, so you need to dereference the arrayref.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Array length
by mifflin (Curate) on Oct 15, 2004 at 04:29 UTC
    Number of arrayrefs in hash element 'one'
    print scalar(@{$a{one}}),"\n";
Re: Array length
by pg (Canon) on Oct 15, 2004 at 05:15 UTC

    Or

    use strict; use warnings; my %a = (one => [[],[],[]]); print $#{$a{"one"}} + 1;

    Indeed what is your @a{one}?

    use Data::Dumper; use strict; use warnings; my %a = (one => [ ['a','b'],['c','d'] ]); print Dumper(@a{one});

    output:

    $VAR1 = [ [ 'a', 'b' ], [ 'c', 'd' ] ];