SSC_perl wrote:
> 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
>
Others have already pointed what you were doing wrong, so I'll point out
something else.
Instead of using 2 separate split statements, I'd use a single split
statement to assign $state and a @zipcodes array.
use 5.010;
use strict;
use warnings;
use Data::Dumper;
my %entries;
while (my $line = <DATA>) {
chomp $line;
my($state, @zipcodes) = split /[=,]/, $line;
push @{ $entries{$state} }, \@zipcodes;
}
foreach my $state (sort keys %entries) {
say "The Zip Codes of $state are";
say Dumper $entries{$state};
}
__DATA__
AK=995,996,997,998,999
CA=95122,95035,95112
-----------
Outputs:
The Zip Codes of AK are
$VAR1 = [
[
'995',
'996',
'997',
'998',
'999'
]
];
The Zip Codes of CA are
$VAR1 = [
[
'95122',
'95035',
'95112'
]
];
--
Ron Bergin
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/