> -----Original Message----- > From: M. Lewis [mailto:[EMAIL PROTECTED] > Sent: Wednesday, December 27, 2006 3:36 PM > To: beginners@perl.org > Subject: 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]; Here, You discard original array ref and replace it with a new one. You should push current item into original array like this: push @{$HoA{$prod}}, $flavor; Here, we add @{...} because push require the first parameter must be an array.
> } > > for my $i (keys %HoA){ > print "$i -- @{ $HoA{$i} }\n"; > } > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>