Recipes for PDSC #1:
Lists of Lists

by Tom Christiansen
< tchrist@perl.com >

release 0.1 (untested, may have typos)
Sunday, 1 October 1995


Note

Please see the expanded version of this documentfor a much more elaborate description of the recipes used herein.

Declaration of a LIST OF LISTS:

@LoL = (        [ "fred", "barney" ],       [ "george", "jane", "elroy" ],       [ "homer", "marge", "bart" ],     );

Generation of a LIST OF LISTS:

# reading from filewhile ( <> ) {    push @LoL, [ split ];}# calling a function for $i ( 1 .. 10 ) {    $LoL[$i] = [ somefunc($i) ];}# using temp varsfor $i ( 1 .. 10 ) {    @tmp = somefunc($i);    $LoL[$i] = [ @tmp ];}# add to an existing rowpush @{ $LoL[0] }, "wilma", "betty";

Access and Printing of a LIST OF LISTS:

# one element$LoL[0][0] = "Fred";# another element$LoL[1][1] =~ s/(\w)/\u$1/;# print the whole thing with refsfor $aref ( @LoL ) {    print "\t [ @$aref ],\n";}# print the whole thing with indicesfor $i ( 0 .. $#LoL ) {    print "\t [ @{$LoL[$i]} ],\n";}# print the whole thing one at a timefor $i ( 0 .. $#LoL ) {    for $j ( 0 .. $#{$LoL[$i]} ) {	print "elt $i $j is $LoL[$i][$j]\n";    }}