http://qs1969.pair.com?node_id=28358

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

I have looked around for the answer to this question, but possibly just over looked it, so just point me to the right place if I missed it. I need to find the length of each array in an array of arrays, here's the code:
for $i (0 .. $#array) { for $k (0 .. $#array[$i]) { print "$array[$i][$k]"; } }
I know this code doesn't work, the for $k (0 .. $#array[$i]) line is simply a representation of what I would like to do.

Replies are listed 'Best First'.
Re: Finding the length of an array of arrays
by btrott (Parson) on Aug 17, 2000 at 23:34 UTC
    I think you want this:
    my @array = ( [ 1..3 ], [ 2..6 ] ); for my $i (0 .. $#array) { for $k (0..$#{ $array[$i] }) { ## This is the key. print $array[$i][$k]; } }
      It is better IMO to encourage the use of foreach rather than C style loops. (Executes faster, faster to write, easier to avoid logic errors like off by one.)
      my @array = ( [ 1..3 ], [ 2..6 ] ); foreach my $outer (@array) { foreach my $element (@$outer) { print "$element\n"; } }
      OTOH map is really nice...
      my @array = ( [ 1..3 ], [ 2..6 ] ); print map {"$_\n"} map {@$_} @array;
      :-)
        Actually, according to Merlyn, at this node for and foreach are the same and have been since the beginning of Perl. That was all cleared up in for vs foreach.

        Minor point of correction... :)
      Shouldn't that be print scalar( @$array[$i][$k] ); or print $#$array[$i][$k]; to print the size out. Otherwise your printing the reference to the array, I think.

      Update: oops, sorry. You're right...

      Got confused with the "length of array in array of arrays". Thought that meant the array of arrays held references to arrays again...
        That print line isn't printing the size; I didn't think that was what the OP wanted, from the code he/she demonstrated. It looked like he/she just wanted to find the "length" of the array (actually the last index) in order to loop over the inner arrays, then print out each element. So that's what my code does: the print line is printing the element with index $k in the array at $i in the original array @array.
Re: Finding the length of an array of arrays
by KM (Priest) on Aug 17, 2000 at 23:40 UTC
    Well, if it is an array of arrays, the arrays will be flattened out like so:

    #!/usr/bin/perl -w use strict; my @a = (1,2,3); my @b = (4,5,6); my @c = (7,8,9,0); my @array = (@a, @b, @c); my $size = @array; # $size will be 10

    If it is an array of array refs, you could do:

    #!/usr/bin/perl -w use strict; my @a = (1,2,3); my @b = (4,5,6); my @c = (7,8,9,0); my @array = (\@a, \@b, \@c); my $i; for (@array) { $i += @$_; } # $i will be 10, scalar @array will be 3

    If it is another scenario, you should now be able to figure out your solution.

    Cheers,
    KM

Re: Finding the length of an array of arrays
by lindex (Friar) on Aug 18, 2000 at 00:21 UTC
    maybe this is what your looking for (recursive array counter) :)
    sub rscalar { my($count); map { $count++; if (ref eq "ARRAY") { $count = $count + rscalar(@{$_}); } } @_; return $count; }



    lindex
    /****************************/ jason@gost.net, wh@ckz.org http://jason.gost.net /*****************************/
RE: Finding the length of an array of arrays
by tenatious (Beadle) on Aug 18, 2000 at 06:05 UTC
    for $element (@{$aref}) { $accumulator += $#{$element} + 1; }