in reply to When hashes aren't enough
OO - as other have said... 'it depends'. I find the question a little unclear, you have items with attributes...
Which are easily encoded as hashes...Car Wheels=4 Doors=4 Color=Blue Bike Wheels=2 Color=Red
but then you look for $Color{"Car"}... rather than $Item{Car}{Color}my %item = ( Car => { Wheels => 4, Doors => 4, Color => 'Blue', }, Bike => { Wheels => 2, Color => 'Red' }, );
so how exactly do you want to use $Item? Do you just want to store the details somewhere? or do you want attribute indexes...
Which is available on CPAN Tie-Hash-TwoWaymy %item = ( Car => { Wheels => 4, Doors => 4, Color => 'Blue', }, Bike => { Wheels => 2, Color => 'Red' }, Moped => { Wheels => 2, Color => 'Blue' }, ); # build an index by attribute to item names... my %attr; for my $name ( keys %item ) { for my $token ( keys %{$item{$name}} ) { $attr{$token}{$item{$name}{$token}} ||= []; push @{$attr{$token}{$item{$name}{$token}}}, $name; } } print "Blue things: @{$attr{Color}{Blue}}\n";
Regards,
Jeff
|
|---|