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

Re^2: Hash of Arrays (updated)

by AnomalousMonk (Archbishop)
on Dec 25, 2020 at 23:35 UTC ( [id://11125740]=note: print w/replies, xml ) Need Help??


in reply to Re: Hash of Arrays
in thread Hash of Arrays

pmilne:   To illustrate a couple of variations on hash-of-arrays:

Win8 Strawberry 5.8.9.5 (32) Fri 12/25/2020 18:25:00 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings use Data::Dumper; use constant { HEIGHT => 0, COLOR => 1, WEIGHT => 2, }; my %HoA; while (<DATA>) { chomp; my ($key, $height, $color, $weight) = split; die "duplicate key '$key'" if exists $HoA{$key}; $HoA{$key} = [ $height, $color, $weight ]; } print Dumper \%HoA; print "'ef56' color is $HoA{'ef56'}[COLOR]"; __DATA__ ef56 2.6 red 4 ef42 2.8 green 3 ^Z $VAR1 = { 'ef56' => [ '2.6', 'red', '4' ], 'ef42' => [ '2.8', 'green', '3' ] }; 'ef56' color is red
C:\@Work\Perl\monks >perl -Mstrict -Mwarnings use Data::Dumper; use constant { HEIGHT => 0, COLOR => 1, WEIGHT => 2, }; my %HoAoA; while (<DATA>) { chomp; my ($key, $height, $color, $weight) = split; push @{ $HoAoA{$key} }, [ $height, $color, $weight ]; } print Dumper \%HoAoA; print "'ef56' second color is $HoAoA{'ef56'}[1][COLOR]"; __DATA__ ef56 2.6 red 4 ef42 2.8 green 3 ef56 9.8 blue 7 ^Z $VAR1 = { 'ef56' => [ [ '2.6', 'red', '4' ], [ '9.8', 'blue', '7' ] ], 'ef42' => [ [ '2.8', 'green', '3' ] ] }; 'ef56' second color is blue

Update: See the Perl Data Structures Cookbook (perldsc) for much more on this topic.


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-25 11:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found