Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Hash of Arrays

by jcb (Parson)
on Dec 26, 2020 at 01:42 UTC ( [id://11125743]=note: print w/replies, xml ) Need Help??


in reply to Hash of Arrays

Assuming multiple items can be inserted under the same key, you probably want a hash of arrays of hashes. Building this is similar, just push a hashref instead of loose values: (all code untested)

push @{ $hash{"ef56"} }, {height => 2.6, color => 'red', weight => 4} +; push @{ $hash{"ef56"} }, {height => 3.2, color => 'blue', weight => 5} +;

This will also allow you to easily iterate over the items associated with any one key:

foreach my $item (@{$hash{"ef56"}}) { print $item->{height}, ' ', $item->{color}, ' ', $item->{weight}, "\ +n"; }

See the documentation for more, particularly perldsc, perllol, perlreftut, and perlref.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-19 12:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found