Thanks John and Zhao,
John W. Krahn wrote:
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;
Ok, but why? Are they not the same?
$HoA{$prod} = [$flavor];
You push them in like so:
push @{ $HoA{ $prod } }, $flavor;
Ok, thanks. This is where I was missing the boat. I'll play around with
this some and see if I can get my head wrapped around it.
Thanks,
Mike
}
for my $i (keys %HoA){
print "$i -- @{ $HoA{$i} }\n";
^^^^^^^^^^^^^
You got it right here. :-)
}
warn Dumper (\%HoA);
__DATA__
[ snip ]
John
--
#define QUESTION ((bb) || !(bb)) - Shakespeare.
19:40:01 up 13 days, 16:31, 0 users, load average: 0.02, 0.13, 0.12
Linux Registered User #241685 http://counter.li.org
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>