You can try using a reference instead of hash like : ------------------ my $entries; my($state, $zipcodes); while (my $line = <DATA>) { chomp $line; ($state, $zipcodes) = split (/=/, $line); push( @{ $entries->{$state} }, split( /,/,$zipcodes) ) }
print Dumper ($entries); __DATA__ AK=995,996,997,998,999 Output: --------- $VAR1 = { 'AK' => [ '995', '996', '997', '998', '999' ] }; I guess you want output something like this . > From: p...@surfshopcart.com > Subject: Creating a hash of arrays from <DATA> > Date: Wed, 9 Jul 2014 18:59:58 -0700 > To: beginners@perl.org > > I'm having a problem creating a hash of arrays. The while loop below > works, but it's working too well. ;) Instead of giving me 5 values (from the > example data below), it's giving me 25. I don't understand why it seems to > be "looping" through the push statement. > > If someone could explain what I'm doing wrong, I'd appreciate it. I > just can't see it. > > Thanks, > Frank > > -------------------------- > > use strict; > use warnings; > use 5.010; > > use Data::Dumper; > > my %entries; > > while (my $line = <DATA>) { > chomp $line; > my($state, $zipcodes) = split (/=/, $line); > push( @{ $entries{$state} }, split( /,/ => $zipcodes) ) > } > > foreach my $state (sort keys %entries) { > say "The Zip Codes of $state are"; > foreach (@{$entries{$state}}) { > print Dumper (@{$entries{$state}}); > } > } > > > __DATA__ > AK=995,996,997,998,999 > > > --OUTPUT-- > The Zip Codes of AK are > $VAR1 = '995'; > $VAR2 = '996'; > $VAR3 = '997'; > $VAR4 = '998'; > $VAR5 = '999'; > $VAR1 = '995'; > $VAR2 = '996'; > $VAR3 = '997'; > $VAR4 = '998'; > $VAR5 = '999'; > $VAR1 = '995'; > $VAR2 = '996'; > $VAR3 = '997'; > $VAR4 = '998'; > $VAR5 = '999'; > $VAR1 = '995'; > $VAR2 = '996'; > $VAR3 = '997'; > $VAR4 = '998'; > $VAR5 = '999'; > $VAR1 = '995'; > $VAR2 = '996'; > $VAR3 = '997'; > $VAR4 = '998'; > $VAR5 = '999'; > > > http://www.surfshopcart.com/ > Setting up shop has never been easier! > > Now on GitHub > https://github.com/surfshopcart/surfshop > > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > >