in reply to Hash of Arrays

Sounds like you rather want a hash of hashes and you already got the appropriate answer.

For completeness: if you're stuck with that HoA you can also opt to use constants:

use constant { height => 0, color => 1 weight=> 2,...}; (untested)

then you'll be able to access the array members by name.

$hash{"ef56"}[height]

If that's also not your question, feel free to clarify.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: Hash of Arrays (updated)
by AnomalousMonk (Archbishop) on Dec 25, 2020 at 23:35 UTC

    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:  <%-{-{-{-<