Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Understading array of hashes

by LanX (Bishop)
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?
[ambrus]: erix: further, SQLite may be the second most installed piece of software on earth after zlib.
[erix]: heh, really? yeah, I suppose it may be so
[ambrus]: We're not really sure. There are some other contenders like libjpeg, and it's not easy to tell how you even measure this.
[ambrus]: But we figured it's close.

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2017-09-25 14:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (280 votes). Check out past polls.

    Notices?