HoA building



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
.



Relevant Pages

  • Re: Charles Aznavour
    ... Or strawberry jam, soaked into the bread. ... My father and I were seriously addicted to eating it by the spoonful, but on baking days my mother had to make not just enough bread to last until the next baking day but she also had to have an extra loaf for my father and me to devour in situ, just barely cooled enough to be sliced, with lashings of butter and Empress jam. ...
    (soc.motss)
  • Re: Somethings Should Never Be Tried
    ... The right way is peanut butter and ... And it HAS to be concord grape. ... If I see another jar of grape jam, ... PLEASE TO BE SENDING ME PEACH PRESERVES THANKING YOU KINDLY ...
    (rec.sport.football.college)
  • Re: Time to start preserving
    ... some Barbara Schaller's mango and strawberry jam with kiwi fruit. ... Maybe strawberry jam or jelly. ... ginger from last year in the freezer that might do well there. ...
    (rec.food.preserving)
  • Re: Charles Aznavour
    ... suddenly reminded that it's Welch's Strawberry Preserves which are ... Historically speaking, I would have to disagree with you, painful as ... with lashings of butter and Empress jam. ... strawberry jam is only available at Frank and Lyndol's, ...
    (soc.motss)
  • Re: anybody here?
    ... > I made Concord Grape Jam for the first time in years this year. ... > received an advanced jar. ... I can sympathize over the labor of grapes. ...
    (rec.food.preserving)