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

Hello Perl'ers

I have an array of arrays I am trying to do this with;
foreach $AoAref (@AoA) { print "Idx last Elmnt=$#{AoAref}\n"; }

but I'm always getting -1 as the length. How can I correctly find the length of an array within an array?

Thankyou in advance.

Replies are listed 'Best First'.
Re: Finding length of array from refrence
by BrowserUk (Patriarch) on Mar 05, 2004 at 13:30 UTC

    You need another $ charcter

    print "Idx last Elmnt=$#{ $AoAref }\n"; # ^

    $#{ ... } says "Give me the highest index of the array who's reference is contained between the curlies".

    So you need to add the variable that holds the reference, in this case $AoAref.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
Re: Finding length of array from refrence
by Limbic~Region (Chancellor) on Mar 05, 2004 at 13:30 UTC
    Anonymous Monk,
    You probably mean one of the following two:
    my $last = $#$AoAref; # index of last element # or my $count = @{ $AoAref }; # Number of elements in array
    Cheers - L~R
Re: Finding length of array from refrence
by Happy-the-monk (Canon) on Mar 05, 2004 at 13:30 UTC
    $element_count = scalar @{ $AoAref }