M. Lewis wrote:
> 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.
> 
> 
> #!/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;

You probably should use ' ' instead of /\s/ as the first argument to split:

     my ($prod, $flavor) = split ' ', $ln, 2;


>     $HoA{$prod} = [$flavor];

You push them in like so:

      push @{ $HoA{ $prod } }, $flavor;


> }
> 
> for my $i (keys %HoA){
>     print "$i -- @{ $HoA{$i} }\n";
                   ^^^^^^^^^^^^^
You got it right here.  :-)


> }
> 
> 
> warn Dumper (\%HoA);
> 
> __DATA__

[ snip ]



John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to