Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Length of 2-dimensional array

by feloniousMonk (Pilgrim)
on Apr 05, 2001 at 22:52 UTC ( #70224=perlquestion: print w/ replies, xml ) Need Help??
feloniousMonk has asked for the wisdom of the Perl Monks concerning the following question:

How do I efficiently find the length of a 2 dimensional array?

I have an array @array[variable#][2] and I need to determine the variable number.

I currently do a foreach with a counter, as a scalar() call or $scalar = @array yield only the width of the array.

Is there a known Perl goodie that can do this, or am I doing this the best way?


Comment on Length of 2-dimensional array
Select or Download Code
Re: Length of 2-dimensional array
by satchboost (Scribe) on Apr 05, 2001 at 23:17 UTC
    Maybe I'm missing something, but wouldn't $#array give you the length of the array? n-dim arrays are really simple arrays, all of whose elements are references to arrays (and so forth for dimensions > 2). Thus, the length is the size of outer-most array. Right?
      First, $#array is the index of the last element in the array. This is never the length of the array itself (unless you've mucked with $[, but don't do that). The best way to get the length of an array is to evaluate it in scalar context.

      Second, they haven't defined what the "length" of the two dimensional array is, so it's very difficult to answer the question. Is "length" the length of the longest sub-array? The number of elements in the the main array? The total of all "end level" elements in the array? So maybe your definition of length is what he needs and maybe it isn't.

      Incidently, Tye already illustrated the issues with the length concepts in another replay: each of the N different Sub-arrays could have different lengths from each other.

(tye)Re: Length of 2-dimensional array
by tye (Cardinal) on Apr 06, 2001 at 00:26 UTC

    $scalar= @array will give you the number of rows (or columns, depending on your convention) and $scalar= @{$array[$i]} will give you the width (or length) of row (or column) number $i.

    There is no single width as each row can have a different width.

            - tye (but my friends call me "Tye")
Re: Length of 2-dimensional array
by jeroenes (Priest) on Apr 06, 2001 at 00:29 UTC
    Your question leaves room for interpretation. Move to strike. A fun answer, though certainly not your favorite (or useful?):
    my $a; # a 2d-array my $count=0; for (@$a){ for (@$_){ $count++; } } print "My 2D-array is $count long\n";

    "We are not alone"(FZ)

      This could also be written as:
      my $a; # a 2d-array my $count=0; for (@$a){ $count += $#$_+1; } print "My 2D-array is $count long\n";
        my $a = [[1,2,3],[4,5,6,7],[3]]; my $count = 0; $count += @$_ for @$a;

        $you = new YOU;
        honk() if $you->love(perl)

        Yes. And extremely's reply spares even more keystrokes. Alls that inspires me to give a somewhat more useful answer:
        $a = [ [ 0..2 ], [0..10],[0..100] ]; my ($min, $max,$sum) = (undef, 0, 0); for (@$a){ $sum += my $a_length = @$_; $min = $a_length if $a_length < $min or not defined $min; $max = $a_length if $a_length > $max; } my $avg = $sum/ @$a; print "Avg: $avg; Min: $min; Max: $max\n";
        Gives some useful array statistics.

        "We are not alone"(FZ)

Re: Length of 2-dimensional array
by feloniousMonk (Pilgrim) on Apr 06, 2001 at 02:00 UTC
    -- Oops. I apparently was not clear enough, many apologies.

    Here's what I have coming into a sub:
    my $data = $_[0]; foreach(@$data){ #These three lines I wanted to replace $length_of_data++; }

    The actual argument passed to the sub is a 2 dim array
    looking like @array[dunno_how_long_this_is][2];
    I need to know how many of element dunno_how_long_this_is there are
    Make better sense?

    The above code works fine, and is efficient enough, I just
    like to use elegant Perl constructs when I'm smart enough
    to figure them out
      I'm sorry, but you are still not clear. Do you want the 'inner' of the 'outer' length?
      1. If you want the inner length (first dimension), just skip the sub and use
        $inner_length = @$data;
      2. If you want the outer (second) dimension, drop the sub as well: $outer_length = @{$data->[$iwannanowthalengthdude]};
      Hope this helps,

      "We are not alone"(FZ)

        OK - that's it. I was looking for the inner length,

        Ijust wasn't seeing it. I've not yet scrubbed all of this
        C from my brain yet so sometimes I get foozeld

        Silly me.

        Fooz-eld - definition: Temporarily confused,
        frustrated. Prelude to disgruntled

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2014-09-02 12:59 GMT
Find Nodes?
    Voting Booth?

    My favorite cookbook is:

    Results (22 votes), past polls