for $x (1 .. 10) {
for $y (1 .. 10) {
for $z (1 .. 10) {
- $LoL[$x][$y][$z] =
+ $LoL[$x][$y][$z] =
$x ** $y + $z;
}
}
you sort it? How can you pass it to a function or get one of these back
from a function? Is is an object? Can you save it to disk to read
back later? How do you access whole rows or columns of that matrix? Do
-all the values have to be numeric?
+all the values have to be numeric?
As you see, it's quite easy to become confused. While some small portion
of the blame for this can be attributed to the reference-based
=back
But for now, let's look at some of the general issues common to all
-of these types of data structures.
+of these types of data structures.
=head1 REFERENCES
you would a real array or hash. For C or C++ programmers unused to distinguishing
between arrays and pointers to the same, this can be confusing. If so,
just think of it as the difference between a structure and a pointer to a
-structure.
+structure.
You can (and should) read more about references in the perlref(1) man
page. Briefly, references are rather like pointers that know what they
point to. (Objects are also a kind of reference, but we won't be needing
-them right away--if ever.) That means that when you have something that
-looks to you like an access to two-or-more-dimensional array and/or hash,
-that what's really going on is that in all these cases, the base type is
+them right away--if ever.) This means that when you have something which
+looks to you like an access to a two-or-more-dimensional array and/or hash,
+what's really going on is that the base type is
merely a one-dimensional entity that contains references to the next
level. It's just that you can I<use> it as though it were a
two-dimensional one. This is actually the way almost all C
for $i (1..10) {
@list = somefunc($i);
$LoL[$i] = @list; # WRONG!
- }
+ }
That's just the simple case of assigning a list to a scalar and getting
its element count. If that's what you really and truly want, then you
for $i (1..10) {
@list = somefunc($i);
$counts[$i] = scalar @list;
- }
+ }
Here's the case of taking a reference to the same memory location
again and again:
for $i (1..10) {
@list = somefunc($i);
$LoL[$i] = \@list; # WRONG!
- }
+ }
So, just what's the big problem with that? It looks right, doesn't it?
After all, I just told you that you need an array of references, so by
rp = getpwnam("root");
dp = getpwnam("daemon");
- printf("daemon name is %s\nroot name is %s\n",
+ printf("daemon name is %s\nroot name is %s\n",
dp->pw_name, rp->pw_name);
}
Which will print
daemon name is daemon
- root name is daemon
+ root name is daemon
The problem is that both C<rp> and C<dp> are pointers to the same location
in memory! In C, you'd have to remember to malloc() yourself some new
memory. In Perl, you'll want to use the array constructor C<[]> or the
hash constructor C<{}> instead. Here's the right way to do the preceding
-broken code fragments
+broken code fragments:
for $i (1..10) {
@list = somefunc($i);
$LoL[$i] = [ @list ];
- }
+ }
The square brackets make a reference to a new array with a I<copy>
of what's in @list at the time of the assignment. This is what
-you want.
+you want.
Note that this will produce something similar, but it's
much harder to read:
for $i (1..10) {
@list = 0 .. $i;
@{$LoL[$i]} = @list;
- }
+ }
Is it the same? Well, maybe so--and maybe not. The subtle difference
is that when you assign something in square brackets, you know for sure
So just remember to always use the array or hash constructors with C<[]>
or C<{}>, and you'll be fine, although it's not always optimally
-efficient.
+efficient.
Surprisingly, the following dangerous-looking construct will
actually work out fine:
for $i (1..10) {
my @list = somefunc($i);
$LoL[$i] = \@list;
- }
+ }
That's because my() is more of a run-time statement than it is a
compile-time declaration I<per se>. This means that the my() variable is
@{ $LoL[$i] } = @list; # way too tricky for most programmers
-=head1 CAVEAT ON PRECEDENCE
+=head1 CAVEAT ON PRECEDENCE
Speaking of things like C<@{$LoL[$i]}>, the following are actually the
same thing:
=head1 DEBUGGING
-The standard Perl debugger in 5.001 doesn't do a very nice job of
-printing out complex data structures. However, the perl5db that
-Ilya Zakharevich E<lt>F<ilya@math.ohio-state.edu>E<gt>
-wrote, which is accessible at
-
- ftp://ftp.perl.com/pub/perl/ext/perl5db-kit-0.9.tar.gz
-
-has several new features, including command line editing as well
-as the C<x> command to dump out complex data structures. For example,
-given the assignment to $LoL above, here's the debugger output:
+Before 5.002, the standard Perl debugger didn't do a very nice job of
+printing out complex data structures. With version 5.002 or above, the
+debugger includes several new features, including command line editing as
+well as the C<x> command to dump out complex data structures. For
+example, given the assignment to $LoL above, here's the debugger output:
DB<1> X $LoL
$LoL = ARRAY(0x13b5a0)
=head1 CODE EXAMPLES
Presented with little comment (these will get their own man pages someday)
-here are short code examples illustrating access of various
+here are short code examples illustrating access of various
types of data structures.
=head1 LISTS OF LISTS
# 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";
# 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";
}
-
+ }
=head1 HASHES OF LISTS
while ( <> ) {
next unless s/^(.*?):\s*//;
$HoL{$1} = [ split ];
-
+ }
# reading from file; more temps
# flintstones: fred barney wilma dino
($who, $rest) = split /:\s*/, $line, 2;
@fields = split ' ', $rest;
$HoL{$who} = [ @fields ];
-
+ }
# calling a function that returns a list
for $group ( "simpsons", "jetsons", "flintstones" ) {
$HoL{$group} = [ get_family($group) ];
-
+ }
# likewise, but using temps
for $group ( "simpsons", "jetsons", "flintstones" ) {
@members = get_family($group);
$HoL{$group} = [ @members ];
-
+ }
# append new members to an existing family
push @{ $HoL{"flintstones"} }, "wilma", "betty";
# print the whole thing
foreach $family ( keys %HoL ) {
print "$family: @{ $HoL{$family} }\n"
-
+ }
# print the whole thing with indices
foreach $family ( keys %HoL ) {
print " $i = $HoL{$family}[$i]";
}
print "\n";
-
+ }
# print the whole thing sorted by number of members
foreach $family ( sort { @{$HoL{$b}} <=> @{$HoL{$b}} } keys %HoL ) {
print "$family: @{ $HoL{$family} }\n"
+ }
# print the whole thing sorted by number of members and name
foreach $family ( sort { @{$HoL{$b}} <=> @{$HoL{$a}} } keys %HoL ) {
print "$family: ", join(", ", sort @{ $HoL{$family}), "\n";
+ }
=head1 LISTS OF HASHES
@LoH = (
{
- Lead => "fred",
- Friend => "barney",
+ Lead => "fred",
+ Friend => "barney",
},
{
Lead => "george",
$rec->{$key} = $value;
}
push @LoH, $rec;
+ }
# reading from file
# no temp
while ( <> ) {
push @LoH, { split /[\s+=]/ };
-
+ }
# calling a function that returns a key,value list, like
# "lead","fred","daughter","pebbles"
while ( %fields = getnextpairset() )
push @LoH, { %fields };
-
+ }
# likewise, but using no temp vars
while (<>) {
push @LoH, { parsepairs($_) };
-
+ }
# add key/value to an element
- $LoH[0]{"pet"} = "dino";
- $LoH[2]{"pet"} = "santa's little helper";
+ $LoH[0]{pet} = "dino";
+ $LoH[2]{pet} = "santa's little helper";
=head2 Access and Printing of a LIST OF HASHES
# one element
- $LoH[0]{"lead"} = "fred";
+ $LoH[0]{lead} = "fred";
# another element
- $LoH[1]{"lead"} =~ s/(\w)/\u$1/;
+ $LoH[1]{lead} =~ s/(\w)/\u$1/;
# print the whole thing with refs
for $href ( @LoH ) {
print "$role=$href->{$role} ";
}
print "}\n";
-
+ }
# print the whole thing with indices
for $i ( 0 .. $#LoH ) {
print "$role=$LoH[$i]{$role} ";
}
print "}\n";
-
+ }
# print the whole thing one at a time
for $i ( 0 .. $#LoH ) {
for $role ( keys %{ $LoH[$i] } ) {
print "elt $i $role is $LoH[$i]{$role}\n";
}
+ }
=head1 HASHES OF HASHES
"pal" => "barney",
},
"jetsons" => {
- "lead" => "george",
- "wife" => "jane",
- "his boy"=> "elroy",
- }
+ "lead" => "george",
+ "wife" => "jane",
+ "his boy" => "elroy",
+ },
"simpsons" => {
"lead" => "homer",
"wife" => "marge",
"kid" => "bart",
- );
+ },
+ );
=head2 Generation of a HASH OF HASHES
($key, $value) = split /=/, $field;
$rec->{$key} = $value;
}
-
+ }
# calling a function that returns a key,value list, like
# "lead","fred","daughter","pebbles"
while ( %fields = getnextpairset() )
push @a, { %fields };
-
+ }
# calling a function that returns a key,value hash
for $group ( "simpsons", "jetsons", "flintstones" ) {
$HoH{$group} = { get_family($group) };
-
+ }
# likewise, but using temps
for $group ( "simpsons", "jetsons", "flintstones" ) {
%members = get_family($group);
$HoH{$group} = { %members };
-
+ }
# append new members to an existing family
%new_folks = (
"wife" => "wilma",
"pet" => "dino";
);
+
for $what (keys %new_folks) {
$HoH{flintstones}{$what} = $new_folks{$what};
-
+ }
=head2 Access and Printing of a HASH OF HASHES
# one element
- $HoH{"flintstones"}{"wife"} = "wilma";
+ $HoH{flintstones}{wife} = "wilma";
# another element
$HoH{simpsons}{lead} =~ s/(\w)/\u$1/;
# print the whole thing
foreach $family ( keys %HoH ) {
print "$family: ";
- for $role ( keys %{ $HoH{$family} } {
+ for $role ( keys %{ $HoH{$family} } ) {
print "$role=$HoH{$family}{$role} ";
}
print "}\n";
-
+ }
# print the whole thing somewhat sorted
foreach $family ( sort keys %HoH ) {
print "$family: ";
- for $role ( sort keys %{ $HoH{$family} } {
+ for $role ( sort keys %{ $HoH{$family} } ) {
print "$role=$HoH{$family}{$role} ";
}
print "}\n";
+ }
# print the whole thing sorted by number of members
foreach $family ( sort { keys %{$HoH{$b}} <=> keys %{$HoH{$b}} } keys %HoH ) {
print "$family: ";
- for $role ( sort keys %{ $HoH{$family} } {
+ for $role ( sort keys %{ $HoH{$family} } ) {
print "$role=$HoH{$family}{$role} ";
}
print "}\n";
-
+ }
# establish a sort order (rank) for each role
$i = 0;
foreach $family ( sort { keys %{$HoH{$b}} <=> keys %{$HoH{$b}} } keys %HoH ) {
print "$family: ";
# and print these according to rank order
- for $role ( sort { $rank{$a} <=> $rank{$b} keys %{ $HoH{$family} } {
+ for $role ( sort { $rank{$a} <=> $rank{$b} keys %{ $HoH{$family} } ) {
print "$role=$HoH{$family}{$role} ";
}
print "}\n";
+ }
=head1 MORE ELABORATE RECORDS
many different sorts:
$rec = {
- STRING => $string,
- LIST => [ @old_values ],
- LOOKUP => { %some_table },
- FUNC => \&some_function,
- FANON => sub { $_[0] ** $_[1] },
- FH => \*STDOUT,
+ TEXT => $string,
+ SEQUENCE => [ @old_values ],
+ LOOKUP => { %some_table },
+ THATCODE => \&some_function,
+ THISCODE => sub { $_[0] ** $_[1] },
+ HANDLE => \*STDOUT,
};
- print $rec->{STRING};
+ print $rec->{TEXT};
print $rec->{LIST}[0];
- $last = pop @ { $rec->{LIST} };
+ $last = pop @ { $rec->{SEQUENCE} };
print $rec->{LOOKUP}{"key"};
($first_k, $first_v) = each %{ $rec->{LOOKUP} };
- $answer = &{ $rec->{FUNC} }($arg);
- $answer = &{ $rec->{FANON} }($arg1, $arg2);
+ $answer = &{ $rec->{THATCODE} }($arg);
+ $answer = &{ $rec->{THISCODE} }($arg1, $arg2);
# careful of extra block braces on fh ref
- print { $rec->{FH} } "a string\n";
+ print { $rec->{HANDLE} } "a string\n";
use FileHandle;
- $rec->{FH}->autoflush(1);
- $rec->{FH}->print(" a string\n");
+ $rec->{HANDLE}->autoflush(1);
+ $rec->{HANDLE}->print(" a string\n");
=head2 Declaration of a HASH OF COMPLEX RECORDS
%TV = (
"flintstones" => {
series => "flintstones",
- nights => [ qw(monday thursday friday) ];
+ nights => [ qw(monday thursday friday) ],
members => [
{ name => "fred", role => "lead", age => 36, },
{ name => "wilma", role => "wife", age => 31, },
- { name => "pebbles", role => "kid", age => 4, },
+ { name => "pebbles", role => "kid", age => 4, },
],
},
"jetsons" => {
series => "jetsons",
- nights => [ qw(wednesday saturday) ];
+ nights => [ qw(wednesday saturday) ],
members => [
{ name => "george", role => "lead", age => 41, },
{ name => "jane", role => "wife", age => 39, },
"simpsons" => {
series => "simpsons",
- nights => [ qw(monday) ];
+ nights => [ qw(monday) ],
members => [
{ name => "homer", role => "lead", age => 34, },
{ name => "marge", role => "wife", age => 37, },
Tom Christiansen E<lt>F<tchrist@perl.com>E<gt>
-Last update:
-Tue Dec 12 09:20:26 MST 1995
-
+Last update:
+Mon Jul 8 05:22:49 MDT 1996