Path: usenet.cis.ufl.edu!usenet.eel.ufl.edu!netline-fddi.jpl.nasa.gov!elroy.jpl.nasa.gov!swrinde!howland.reston.ans.net!news.sprintlink.net!in1.uu.net!csnews!alumni.cs.colorado.edu!tchrist
From: Tom Christiansen <
[email protected]>
Newsgroups: comp.lang.perl.misc,comp.lang.perl.announce
Subject: PDSC Recipes for Lists of Lists (part 1 of 5)
Followup-To: comp.lang.perl.misc
Date: 2 Oct 1995 01:50:33 GMT
Organization: Perl Consulting and Training
Lines: 65
Approved:
[email protected]
Message-ID: <
[email protected]>
References: <
[email protected]>
Reply-To:
[email protected] (Tom Christiansen)
NNTP-Posting-Host: alumni.cs.colorado.edu
Originator:
[email protected]
Xref: usenet.cis.ufl.edu comp.lang.perl.misc:7635 comp.lang.perl.announce:139
PDSC: The Perl Data Structures Cookbook
part 1 of 5: Lists of Lists
by Tom Christiansen
<
[email protected]>
release 0.1 (untested, may have typos)
Sunday, 1 October 1995
Declaration of a LIST OF LISTS:
@LoL = (
[ "fred", "barney" ],
[ "george", "jane", "elroy" ],
[ "homer", "marge", "bart" ],
);
Generation:
# reading from file
while ( <> ) {
push @LoL, [ split ];
}
# calling a function
for $i ( 1 .. 10 ) {
$LoL[$i] = [ somefunc($i) ];
}
# using temp vars
for $i ( 1 .. 10 ) {
@tmp = somefunc($i);
$LoL[$i] = [ @tmp ];
}
# add to an existing row
push @{ $LoL[0] }, "wilma", "betty";
Access and Printing:
# one element
$LoL[0][0] = "Fred";
# another element
$LoL[1][1] =~ s/(\w)/\u$1/;
# print the whole thing with refs
for $aref ( @LoL ) {
print "\t [ @$aref ],\n";
}
# print the whole thing with indices
for $i ( 0 .. $#LoL ) {
print "\t [ @{$LoL[$i]} ],\n";
}
# print the whole thing one at a time
for $i ( 0 .. $#LoL ) {
for $j ( 0 .. $#{$LoL[$i]} ) {
print "elt $i $j is $LoL[$i][$j]\n";
}
}