Nested Data Structures

Hashes of hashes:

%h = ( "1d" => { name => "MOT1"}, 
       "2d" => { name => "MOT2"}, 
       "3d" => { name => "MOT3"}); 

print "Motor " . $h{ "2d"}{ name} . "\n";
# prints 'MOT1'

The following example demonstrates how array references can be stored in hashes:

%sue   = ( name => 'Sue', age => 45); # parent
%john  = ( name => 'John', age => 20); # child
%peggy = ( name => 'Peggy', age => 16); # child

@children = (\%john, \%peggy); 
$sue{ children} = \@children; 
# or
$sue{ children} = [\%john, \%peggy];

print "Peggy's age: " . $sue{ children}->[ 1]->{ age} . "\n";
print "Peggy's age: " . $sue{ children}[ 1]{ age} . "\n"; # shortcut
#
# implicit creation of complex data structures: the following
# statement is valid without any preceding initialization of 
# the data structure
#
$sue{children}[1]{age} = 10;
#
# using anonymous arrays
#
%sue = 
(
  name => 'Sue', age => 45, 
  children => 
    [
      { name => 'John', age => 20}, 
      { name => 'Peggy', age => 16},
    ],
)

Here is an example for a data record:

$rec = {
  TEXT     => $string, 
  SEQUENCE => [ @old_values ], 
  LOOKUP   => [ %some_table],
  THATCODE => \&some_function, 
  THISCODE => sub { $_[0] ** $_[1]}, 
  HANDLE   => \*STDOUT,
}
print $rec->{ TEXT};  
print $rec->{ SEQUENCE}[ 0]; 
print $rec->{ LOOKUP}{ "key"}; 
($first_k, $first_v) = each %{ $rec->{ LOOKUP}}; 
$answer = &{ $rec->{ THATCODE}} ( $arg); 
$answer = &{ $rec->{ THISCODE}} ( $arg1, $arg2); 
print { $rec->{ HANDLE} } "a string\n";