Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Understading array of hashes

by LanX (Chancellor)
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1029973]
help
Chatterbox?
[LanX]: interpolation! like in a "string"
[Lady_Aleena]: LanX, I just never thought I would be explaining perl regex to others. It is usually others explaining why a regex isn't working to me.
[Discipulus]: congratulations shmem! (was my last upvote? ;=)

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2017-04-28 09:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (520 votes). Check out past polls.