Complex structures

When you really do something serious, you will no doubt need to use a complex data structure, such has a hash of arrays of hashes.

This isn’t as unusual as it sounds, as most Perl Objects use a Hash Reference as a handle. The elemenets of the hash are the instance variables/values for the object. If one of these was a database result set as given above, then you need to reference the data structure like this.

my $self = {}; # In object constructor
bless $self, $className; # Makes it an object of this type
 
$self->{table} = selectRows(...); # ref to array of hashes
print $self->{table}[0]{name}; # access first row, name col
 
$self->{table}[0]{jobs} = selectRows(...);
print $self->{table}[0]{jobs}[0]{company};
foreach my $jobrow (@{$self->{table}[0]{jobs}}) {
  print $jobrow->{company};
}
 
programming/perl/dstruc.txt · Last modified: 2005/07/18 17:15 by allen