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: Perl Data Structures Cookbook: General Tips (part 0 of 5)
Followup-To: comp.lang.perl.misc
Date: 2 Oct 1995 01:47:54 GMT
Organization: Perl Consulting and Training
Lines: 121
Approved:
[email protected]
Message-ID: <
[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:7634 comp.lang.perl.announce:138
PDSC: The Perl Data Structures Cookbook
by Tom Christiansen
<
[email protected]>
part 0 of 5
release 0.1 (untested, may have typos)
Sunday, 1 October 1995
This is a cookbook of recipes for building up complex data structures
in perl5. It has been extracted from a much larger and more
expository document to be published in pod format and included with
the standard perl distribution. The goal is to provide cookbook-like,
cut-and-paste examples of the most often used data structures in
perl. Think of the recipes as a quick reference
It currently has 6 parts:
0: Perl Data Structures Cookbook: General Tips [this document]
1: PDSC recipes: Lists of Lists
2: PDSC recipes: Hashes of Lists
3: PDSC recipes: Lists of Hashes
4: PDSC recipes: Hashes of Hashes
5: PDSC recipes: More Elaborate Structures
The first thing you need to do is figure out how you want to access
(such as via an assignment) just ONE INDIVIDUAL element of your
data structure just using lists and hashes. Use a list if you're
thinking of an array, use a hash if you're thinking of a record
or a lookup table.
1) $coordinates[$row][$col] = "empty"
This is a simple two-dimensionsal array indexed
by integers. See the List of Lists document.
2) $student[$i]{age} = 15;
This is a array of records that include named fields.
See the List of Hashes document.
3) $flight_time{$destination}[3] = "12:34";
This is data structure indexed by a target destination (a string
name, like "denver") to produce an array of several possible
flight times. See the Hash of Lists document.
4) $tv_shows{"the simpsons"}{"start time"} = "Monday 20:00";
This is an lookup table of records where you lookup the
show by the name, and you look up the record field by
the field name. See the Hash of Hashes document.
5) $tv{"the simpsons"}{members}[0]{name} = "homer";
This is an elaborate data structure involving a mix of
records that contain fields that are sometimes themselves
other arrays and records. See the Hash of Complex Records
document.
Here are some further tips of general interest:
* Always use strict and -w. The strict can be a pain,
but it will save you from saying $a[$i] when you
mean $a->[$i] and vice versa.
* Things like push require an @ sign, as in
push @{ $a{key} }, @new_list;
You can't write
push $a{key}, @new_list;
* Things like keys require a % sign, as in
foreach $k (keys %{ $h{key} }) { ... }
You can't write
foreach $k (keys $h{key}) { ... }
* Don't store pointers to existing data in a structure.
Always create a new structure, eg. to build a 2D array
indexed by line and by word number:
while ( <> ) {
@fields = split;
push @a, [ @fields ];
}
This generally means never using the backslash to take
a reference, but rather using the [] or {} constructors.
This, for examples, is wrong:
while ( <> ) {
@fields = split;
push @a, \@fields;
}
* Never write $$a[$i] when you mean $a->[$i]. It'll work,
but will confuse.
* Remember that $a[$i] is the i'th elt of @a, but $a->[$i]
is the i'th elt of the anon array pointed to by $a. use
strict will help here.
* Never write $$a[$i] when you mean ${$a[$i]} or
@$a[$i] when you mean @{$a[$i]}.
Those won't work at all.
* Never write
@ { $a[$i] } = @list;
instead of
$a[$i] = [ @list ];
It'll work, but will confuse people.
* Try to use pointer arrows and ${bracketing} whenever you
feel the reader might be confused. Sometimes it'll clear
things up in your mind as well.
* The new perl5db (ftp to perl.com in /pub/perl/ext/) will
help print out complex data structures using the x and X
commands.