Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: When hashes aren't enough

by jaa (Friar)
on May 25, 2004 at 17:28 UTC ( [id://356303]=note: print w/replies, xml ) Need Help??


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...

Car Wheels=4 Doors=4 Color=Blue Bike Wheels=2 Color=Red
Which are easily encoded as hashes...
my %item = ( Car => { Wheels => 4, Doors => 4, Color => 'Blue', }, Bike => { Wheels => 2, Color => 'Red' }, );
but then you look for $Color{"Car"}... rather than $Item{Car}{Color}

so how exactly do you want to use $Item? Do you just want to store the details somewhere? or do you want attribute indexes...

my %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";
Which is available on CPAN Tie-Hash-TwoWay

Regards,

Jeff

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://356303]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-18 03:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found