Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: hash of hashes

by AnomalousMonk (Abbot)
on Oct 06, 2013 at 05:21 UTC ( #1057117=note: print w/ replies, xml ) Need Help??


in reply to Re^2: hash of hashes
in thread hash of hashes

You seem to be pretty much at sea on this, so here's a bit of an elaboration on marinersk's approach. I'm assuming you can read "statements" from a file, get rid of newlines (see chomp), write data to a file, etc.

>perl -wMstrict -le "use Data::Dumper; ;; my @statements = ( 'strawberry red green rose', 'apple red rose', 'mango green', ); ;; my %hash; for my $statement (@statements) { my ($fruit, @colors) = split m{\s+}xms, $statement; for my $color (@colors) { $hash{$fruit}{$color} = 1; } } ;; print Dumper \%hash; " $VAR1 = { 'mango' => { 'green' => 1 }, 'strawberry' => { 'rose' => 1, 'green' => 1, 'red' => 1 }, 'apple' => { 'rose' => 1, 'red' => 1 } };

See perldsc for a discussion of techniques for iterating through a data structure of this (hash-of-hashes, HoH) or any other kind. As others have said, we would be happy to answer further questions, but the community really likes to see supporting work as well.

Update: Also see Re: hash of hashes for a different, perhaps more directly expressive data structure.


Comment on Re^3: hash of hashes
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (15)
As of 2015-07-06 20:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (81 votes), past polls