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

Hi all, I'm have an array of arrays in which I am trying to find the number of indices for any of the referenced arrays(they should all be the same). This list of arrays if returned from a mysql query. If I do something like this:
foreach my $row (@$list) { print "$#{$row} <br>"; ..... }
it works. The problem is I need to have the given number before I enter a foreach loop. So I figured this would work.
my $dref = $@list; my $num = "$#{list}"
But it doesn't. This is for a cgi script, and adding those lines causes this to be printed in my error log:
Can't use string ("8") as an ARRAY ref while "strict refs" in use at / +usr/lib/perl5/5.6.1/whiteware.pm line 109.
I'm so confused after reading the perlref and perllol man pages. Can anyone please explain to me what's going on? Thanks, Elam

Replies are listed 'Best First'.
Re: Last element index with array refs
by sauoq (Abbot) on Feb 10, 2003 at 01:45 UTC
    I'm have an array of arrays in which I am trying to find the number of indices for any of the referenced arrays(they should all be the same).

    Well, that's not what the title of your node asks for, but it is easy enough...

    my @AoA = ( [1,3,5,7] ); print scalar @{$AoA[0]}; # 4
    To get what the title asks for, the last element index, you can use...
    print $#{$AoA[0]}; # 3

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Last element index with array refs
by elam (Acolyte) on Feb 09, 2003 at 22:27 UTC
    Hmmm, I think I got it now:
    my $dref = @$list->[0]; my $num = $#{$dref};

      No. Certainly not. Contrary to popular belief, you should not use @ when getting a value from an array. Just as you don't do @array[0], but rather $array[0]. You're using a sigil and the arrow, and are thus doubly dereferencing. The arrow is the dereferencing operator. That means you don't have to put any other extra sigil in front of the reference. See Re: Re: passing object references into subs for another reply of mine covering the very same issue, and gives pointers to documentation that lets you know that this isn't supposed to work, but mysteriously it does anyway.

      ihb
Re: Last element index with array refs
by elam (Acolyte) on Feb 09, 2003 at 22:09 UTC
    Hit submit too soon, should be:
    my $dref = @$list; my $num = "$#{$dref}"
      my $dref = @$list;
      $dref is a scalar variable, so it imposes scalar context on the right-hand side of the assignment. An array used in scalar context returns the number of elements it contains. That's where your string "8" comes from.

      To get what you want, you have to use my ($dref) = @$list; which assigns the first element of the array to $dref. Then you can get the length of this array reference.

      If you want to make sure that all the arrays are of the same length your only solution is a foreach loop (or something equivalen). On the other hand, if you are positive that all subarrays have the same length, this method of just looking at the first element works to solve your problem.

      -- Hofmator