in reply to work with Array of Arrays
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#!/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"; }
for(my $i = 0; $i<=$#AoA;$i++){ my $count = @{$AoA[$i]}; print "Row #: ", $i+1, " has $count columns\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: work with Array of Arrays
by almut (Canon) on Apr 18, 2010 at 08:03 UTC |