in reply to Complex Data Structures?
I've done this before like so:
Note how easy it is to extend to index on more fields. If this confuses you, then you can change to code to prepend "By" to each field name for indices so you write:my %People; while( my $id= GetNextId() ) { my( $name, $age, $addr, $city, $state, $phone )= GetDataForId( $id ); my $record= { NAME=>$name, AGE=>$age, ADDR=>$addr, CITY=>$city, STATE=>$state, PHONE=>$phone }; warn "Duplicate ID numbers: $People{ID}{$id} vs. $name\n" if $People{ID}{$id}; $People{ID}{$id}= $record; foreach my $field ( qw( NAME AGE STATE PHONE ) ) { push @{ $People{$field}{ $record->{$field} } }, $record; } } print "(The first) John Smith is ", "$People{NAME}{'John Smith'}[0]{AGE} years old.\n"; print "There are ",0+@{$People{AGE}{20}}, " 20-year-olds:\n"; foreach my $person ( @{ $People{AGE}{20} } ) { print "\t",$person->{NAME},"\n"; }
instead.print "(The first) John Smith is ", "$People{ByNAME}{'John Smith'}[0]{AGE} years old.\n";
Note how each level is a reference so if you are dealing with one part over and over you can make short cuts:
and that each of the many ways of getting at the data all get to the same copy of the data so you aren't wasting space and can update the data via any way you get to it. - tye (but my friends call me "Tye")my $byID= $People{ID}; foreach my $person ( @$byID ) { }
|
|---|