purloin a drop-in example from here.
Let's look at each of these possible constructs in detail. There are separate
-documents on each of the following:
+sections on each of the following:
=over 5
=item * more elaborate constructs
-=item * recursive and self-referential data structures
-
-=item * objects
-
=back
But for now, let's look at some of the general issues common to all
for $i (1..10) {
@list = somefunc($i);
- $counts[$i] = scalar @list;
+ $counts[$i] = scalar @list;
}
Here's the case of taking a reference to the same memory location
2 'elroy'
3 'judy'
-There's also a lower-case B<x> command which is nearly the same.
+There's also a lowercase B<x> command which is nearly the same.
=head1 CODE EXAMPLES
-Presented with little comment (these will get their own man pages someday)
+Presented with little comment (these will get their own manpages someday)
here are short code examples illustrating access of various
types of data structures.
# print the whole thing one at a time
for $i ( 0 .. $#LoL ) {
- for $j ( 0 .. $#{$LoL[$i]} ) {
+ for $j ( 0 .. $#{ $LoL[$i] } ) {
print "elt $i $j is $LoL[$i][$j]\n";
}
}
=head2 Declaration of a HASH OF LISTS
%HoL = (
- "flintstones" => [ "fred", "barney" ],
- "jetsons" => [ "george", "jane", "elroy" ],
- "simpsons" => [ "homer", "marge", "bart" ],
+ flintstones => [ "fred", "barney" ],
+ jetsons => [ "george", "jane", "elroy" ],
+ simpsons => [ "homer", "marge", "bart" ],
);
=head2 Generation of a HASH OF LISTS
}
# print the whole thing sorted by number of members
- foreach $family ( sort { @{$HoL{$b}} <=> @{$HoL{$b}} } keys %HoL ) {
+ foreach $family ( sort { @{$HoL{$b}} <=> @{$HoL{$a}} } 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 ) {
+ foreach $family ( sort {
+ @{$HoL{$b}} <=> @{$HoL{$a}}
+ ||
+ $a cmp $b
+ } keys %HoL )
+ {
print "$family: ", join(", ", sort @{ $HoL{$family}), "\n";
}
=head2 Declaration of a HASH OF HASHES
%HoH = (
- "flintstones" => {
- "lead" => "fred",
- "pal" => "barney",
+ flintstones => {
+ lead => "fred",
+ pal => "barney",
},
- "jetsons" => {
- "lead" => "george",
- "wife" => "jane",
- "his boy" => "elroy",
+ jetsons => {
+ lead => "george",
+ wife => "jane",
+ "his boy" => "elroy",
},
- "simpsons" => {
- "lead" => "homer",
- "wife" => "marge",
- "kid" => "bart",
+ simpsons => {
+ lead => "homer",
+ wife => "marge",
+ kid => "bart",
},
);
# append new members to an existing family
%new_folks = (
- "wife" => "wilma",
- "pet" => "dino";
+ wife => "wilma",
+ pet => "dino";
);
for $what (keys %new_folks) {
# print the whole thing sorted by number of members
- foreach $family ( sort { keys %{$HoH{$b}} <=> keys %{$HoH{$b}} } keys %HoH ) {
+ foreach $family ( sort { keys %{$HoH{$b}} <=> keys %{$HoH{$a}} } keys %HoH ) {
print "$family: { ";
for $role ( sort keys %{ $HoH{$family} } ) {
print "$role=$HoH{$family}{$role} ";
for ( qw(lead wife son daughter pal pet) ) { $rank{$_} = ++$i }
# now print the whole thing sorted by number of members
- foreach $family ( sort { keys %{$HoH{$b}} <=> keys %{$HoH{$b}} } keys %HoH ) {
+ foreach $family ( sort { keys %{ $HoH{$b} } <=> keys %{ $HoH{$a} } } 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";
print $rec->{LOOKUP}{"key"};
($first_k, $first_v) = each %{ $rec->{LOOKUP} };
- $answer = &{ $rec->{THATCODE} }($arg);
- $answer = &{ $rec->{THISCODE} }($arg1, $arg2);
+ $answer = $rec->{THATCODE}->($arg);
+ $answer = $rec->{THISCODE}->($arg1, $arg2);
# careful of extra block braces on fh ref
print { $rec->{HANDLE} } "a string\n";
=head2 Declaration of a HASH OF COMPLEX RECORDS
%TV = (
- "flintstones" => {
+ flintstones => {
series => "flintstones",
nights => [ qw(monday thursday friday) ],
members => [
],
},
- "jetsons" => {
+ jetsons => {
series => "jetsons",
nights => [ qw(wednesday saturday) ],
members => [
],
},
- "simpsons" => {
+ simpsons => {
series => "simpsons",
nights => [ qw(monday) ],
members => [
foreach $family (keys %TV) {
$rec = $TV{$family}; # temp pointer
@kids = ();
- for $person ( @{$rec->{members}} ) {
+ for $person ( @{ $rec->{members} } ) {
if ($person->{role} =~ /kid|son|daughter/) {
push @kids, $person;
}
for $who ( @{ $TV{$family}{members} } ) {
print " $who->{name} ($who->{role}), age $who->{age}\n";
}
- print "it turns out that $TV{$family}{'lead'} has ";
+ print "it turns out that $TV{$family}{lead} has ";
print scalar ( @{ $TV{$family}{kids} } ), " kids named ";
print join (", ", map { $_->{name} } @{ $TV{$family}{kids} } );
print "\n";
Berkeley DB have size limitations, but beyond that, you also have problems
with how references are to be represented on disk. One experimental
module that does partially attempt to address this need is the MLDBM
-module. Check your nearest CPAN site as described in L<perlmod> for
+module. Check your nearest CPAN site as described in L<perlmodlib> for
source code to MLDBM.
=head1 SEE ALSO
=head1 AUTHOR
-Tom Christiansen E<lt>F<tchrist@perl.com>E<gt>
+Tom Christiansen <F<tchrist@perl.com>>
Last update:
-Mon Jul 8 05:22:49 MDT 1996
+Wed Oct 23 04:57:50 MET DST 1996