home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


B.2 Nested Data Structures

Each array or hash is a collection of scalars, some or all of which can be references to other structures.

  1. Lists do not nest like this:

    @foo = (1, 3, ("hello", 5), 5.66);

    For nesting, make the third element a reference to an anonymous array:

    @foo = (1, 3, ["hello", 5], 5.66);
          
  2. An example of a nested data structure (a hash of array of hashes):

    $person = {   # Anon. hash
       "name" => "John",  # '=>' is an alias for a comma
       "age"  => 23,
       "children" => [  # Anonymous array of children
                         {
                            "name" => "Mike",  "age"  => 3,
                         },
                         {
                            "name"  => "Patty","age"   => 4
                         }
                     ]
       };
    print $person->{age}                 ; # Print John's age
    print $person->{children}->[0]->{age}; # Print Mike's age
    print $person->{children}[0]{age}    ; # Print Mike's age, omitting
                                           # arrows between subscripts
  3. To pretty-print $person above:

     use Data::Dumper;
     Data::Dumper->Dumper($person);
     # Or,
     require dumpVar.pl;
     main::dumpValue($person);