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