It is relatively easier to print the number of rows and columns for a list of lists if all columns for each row are of the same count, if that is not the case then it is possible with a little bit more lines to count the number of columns for individual rows, I would advice you to read
References quick reference and also these ones here
perlcheat,
perlref and
perlreftut for related tutorials and quick cheats on references...
#!/usr/local/bin/perl
use strict;
use warnings;
my @AoA=(
[0, 1, 0, 0],
[0, 0, 1, 0,0,0,0,0],
[0, 0, 0, 1,0,1]
);
# my $row = scalar @AoA; ##Prints the number of rows for the array
for(my $i = 0; $i<=$#AoA;$i++){
my $count =0;
foreach my $column (@{$AoA[$i]}){
$count ++;
}
print "Row# ", $i+1, " has $count columns\n";
}
Update: On the behest of
almut's reply, the above code is just a prove of concept to ways to generally iterate over lists of lists, I wrote it in a rush while thinking of an approach similar to
almut's, my code above unnecessarily loops over array elements just to do an incrementation... Here's a more efficient approach
for(my $i = 0; $i<=$#AoA;$i++){
my $count = @{$AoA[$i]};
print "Row #: ", $i+1, " has $count columns\n";
}
Excellence is an Endeavor of Persistence.
Chance Favors a Prepared Mind.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.