in reply to work with Array of Arrays

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.

Replies are listed 'Best First'.
Re^2: work with Array of Arrays
by almut (Canon) on Apr 18, 2010 at 08:03 UTC
    my $count =0; foreach my $column (@{$AoA[$i]}){ $count ++; }

    When all you do in the loop is increment, you might as well just write

    my $count = @{$AoA[$i]};

    which, btw, is approximately N times faster, with N being the size of the array:

    use Benchmark 'cmpthese'; my @AoA; $AoA[42] = [ (1) x 1000 ]; cmpthese(-1, { counted => sub { my $count =0; foreach my $column (@{$AoA[42]}){ $count ++; } }, direct => sub { my $count = @{$AoA[42]}; }, } ); __END__ Rate counted direct counted 11821/s -- -100% direct 9267717/s 78297% --