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

What is the simplest way to count the number of columns from an array of arrays?

For instance, how can I find out how many members are in @list[0] given:

     @list=(["1",2","3"],["a","b","c"]);

Replies are listed 'Best First'.
Re: Enumerate an array of arrays?
by marcussen (Pilgrim) on Feb 06, 2008 at 01:27 UTC
    scalar(@{$list[0]});
    should do the trick.

    while ( whoring ){ for ( xp ){ grep /the source/,@perlmonks; }}
      Perfect! Thanks.
Re: Enumerate an array of arrays?
by GrandFather (Saint) on Feb 06, 2008 at 02:44 UTC

    A few other ways of accessing your rows and elements are:

    use strict; use warnings; my @list=(["1","2","3"],["a","b","c"]); print "A row:\n"; for my $element (@{$list[0]}) { print "$element\n"; } print "or stringify row: @{$list[0]}\n\n"; print "All rows:\n"; for my $row (@list) { print "@$row\n"; } print "\nAll rows by index:\n"; for my $rowIndex (0 .. $#list) { print "row $rowIndex: @{$list[$rowIndex]}\n"; }

    Prints:

    A row: 1 2 3 or stringify row: 1 2 3 All rows: 1 2 3 a b c All rows by index: row 0: 1 2 3 row 1: a b c

    Perl is environmentally friendly - it saves trees