Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How do I find the size of each row of a growing multidimensional array?

by ozgurp (Beadle)
on Mar 03, 2005 at 23:04 UTC ( #436398=perlquestion: print w/replies, xml ) Need Help??

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

Hi All, I have a multidimensional array that can grow or decrease in size in both directions (I mean the number of rows and the number of columns), How can I find the size of each row (the number of columns in each row)? The following statement gives me only the number of rows but I cannot get number of columns in each row:
my $size = @INFO_ARRAY;
Thanks.
  • Comment on How do I find the size of each row of a growing multidimensional array?
  • Download Code

Replies are listed 'Best First'.
Re: How do I find the size of each row of a growing multidimensional array?
by Zed_Lopez (Chaplain) on Mar 03, 2005 at 23:13 UTC

    If you've implemented your multidimensional array as an array of arrayrefs, and if every row has the same number of columns:

    my $num_columns = @{$INFO_ARRAY[0]};

    But, since I don't really know how you've implemented it, I'm just guessing.

Re: How do I find the size of each row of a growing multidimensional array?
by mlh2003 (Scribe) on Mar 03, 2005 at 23:36 UTC
    When you say you want to find the number of columns in each row, do you mean that you (can) have a different number of columns in each row? As Zed_Lopez mentioned, if all rows are the same size, then a single equality on the first row is all that is needed. Otherwise you'd need to loop through the array and extract the number of columns for each row. The following code again assumes you have an array of array refs:
    $row = 1; foreach (@INFO_ARRAY) { print "Row $row has " . @{$INFO_ARRAY[$_]} . "columns.\n"; $row++; }
    Another solution (if you wanted to retain those values) would be to populate another array with the number of elements (columns) in each row of INFO_ARRAY...
    --------------------
    mlh2003
      Sorry, I was not specific about my multidimensional array. In my case each row had same number of columns containing integers. However it is useful to know if this was not the case.
Re: How do I find the size of each row of a growing multidimensional array?
by TedPride (Priest) on Mar 04, 2005 at 03:32 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://436398]
Approved by kvale
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2021-11-27 12:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?