Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
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?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2016-07-01 12:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...











    Results (411 votes). Check out past polls.