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

how to find the number of rows in a double dimension array ?

thanks in advance

  • Comment on number of rows in double dimension array

Replies are listed 'Best First'.
Re: number of rows in double dimension array
by kcott (Archbishop) on Jul 24, 2013 at 09:58 UTC

    That would depend on how you arrange the rows and columns in your array; however, if you use (what I'd consider) the normal way, then @double_dimension_array in scalar context will return the number of rows. Furthermore, assuming a gridded or table structure, where each row has the same number of columns, @{ $double_dimension_array[0] } in scalar context will return the number of columns.

    $ perl -Mstrict -Mwarnings -E ' my @dd = ( [ qw{ r0c0 r0c1 r0c2 } ], [ qw{ r1c0 r1c1 r1c2 } ], ); my $number_of_rows = @dd; say "No. of rows: $number_of_rows"; my $number_of_columns = @{ $dd[0] }; say "No. of columns: $number_of_columns"; ' No. of rows: 2 No. of columns: 3

    -- Ken

Re: number of rows in double dimension array
by hdb (Monsignor) on Jul 24, 2013 at 09:50 UTC

    A two-dimensional array in Perl usually is an array of array references, so the number of rows is just the number of elements in the "outer" array:

    my @dd = ( [ 11, 12, 13], [ 21, 22, 23], [ 31, 32, 33], [41, 42, 43], +); print "Number of rows: ", scalar @dd, "\n";