| ![]() | |||||||||
$student[$i]{"age"} = 15
This is an array of records that include named fields. Each first level numeric index itself produces
a hash (reference). See the List of Hashes document.
$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 then you look up
the record field by the field name. Each first level string index itself produces a hash (reference).
See the Hash of Hashes document.
$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 More Elaborate Structures document.
print {$rec->{FH}}
&{ $rec->{FUNC} } ( $rec->{LIST}[0] )
This is a strange record that itself includes references to filehandles, functions, and other strings,
lists, and hashes. We print to the filehandle referenced in $rec's FH the result of calling the
function in its FUNC field with an argument of the first element in the array which is its LIST
field. See also the More Elaborate Structures document.
General Tips
Here are some further tips of general interest:
1. 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.
2. Things like push() require an @ sign, as in push @{ $a[3] }, @new_list
You can't write
push $a[3], @new_list
3. Things like keys() require a % sign, as in foreach $k (keys %{ $h{"key"} }) { ... }
You can't write
foreach $k (keys $h{"key"}) { ... }
4. 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!