HoA building
- From: cajun@xxxxxxxxxxxx ("M. Lewis")
- Date: Wed, 27 Dec 2006 02:35:57 -0500
I have the following simple script. I'm toying with HoA for the first time. The script is not working as expected and I know why it's not working. Each time a $HoA{$prod} is read, the $flavor is replaced with the new value.
It would seem that I need to push the $flavor onto an array. But I'm not quite sure how to proceed.
Pointers greatly appreciated.
Thanks,
Mike
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper::Simple;
my %HoA;
while (my $ln = <DATA>){
chomp $ln;
my ($prod, $flavor) = split /\s/, $ln, 2;
$HoA{$prod} = [$flavor];
}
for my $i (keys %HoA){
print "$i -- @{ $HoA{$i} }\n";
}
warn Dumper (\%HoA);
__DATA__
jelly strawberry
jelly apple
jelly pear
jelly grape
jam strawberry
jam apple
jam grape
milk plain
milk chocolate
milk strawberry
OUTPUT:
jelly -- grape
jam -- grape
milk -- strawberry
$HoA = {
'jelly' => [
'grape'
],
'jam' => [
'grape'
],
'milk' => [
'strawberry'
]
};
Desired output:
jelly -- strawberry
-- apple
-- pear
-- grape
jam -- strawberry
-- apple
-- grape
milk -- plain
-- chocolate
-- strawberry
--
God is real, unless declared integer.
02:20:01 up 12 days, 23:11, 0 users, load average: 0.20, 0.39, 0.35
Linux Registered User #241685 http://counter.li.org
.
- Follow-Ups:
- RE: HoA building
- From: zhao_bingfeng
- Re: HoA building
- From: John W. Krahn
- RE: HoA building
- Prev by Date: Re: Remove an element from middle of array
- Next by Date: Re: what's mistaken?
- Previous by thread: what's mistaken?
- Next by thread: Re: HoA building
- Index(es):
Relevant Pages
|
|