Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Understading array of hashes

by LanX (Canon)
on Apr 22, 2013 at 20:17 UTC ( #1029973=note: print w/ replies, xml ) Need Help??


in reply to Understading array of hashes

I think you are looking for a hash of arrays

DB<126> $array => [ { name => "Discount", reference => "100 ", type => "Paper" }, { name => "Documents", reference => "100 ", type => "Paper" }, { name => "Money", reference => "340 ", type => "Plastic" }, { name => "State", reference => "40 ", type => "Cotton" }, { name => "Slice", reference => "30 ", type => "Cotton" }, { name => "Part", reference => "45 ", type => "Cotton" }, ] DB<127> push @{ $hash{$_->{type}} }, $_->{name} for @$array; DB<128> \%hash => { Cotton => ["State", "Slice", "Part"], Paper => ["Discount", "Documents"], Plastic => ["Money"], }

update

DB<136> use YAML DB<137> print Dump \%hash --- Cotton: - State - Slice - Part Paper: - Discount - Documents Plastic: - Money

Cheers Rolf

( addicted to the Perl Programming Language)

PS: Starting with Perl 5.14, push can take a scalar EXPR, which must hold a reference to an unblessed array. ... i.a.W. you won't need the @{...} part.


Comment on Re: Understading array of hashes
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2014-09-23 22:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (241 votes), past polls