hash values

J

jim.goodman

i am having a tough time getting values to print.... below is my code
with output to follow (it's wrong, i can't get the "values"... either i
am doing something wrong in the "print" loop or in acquiring the data,
both are a line or two, if you could please have a look... thanks :eek:)

#!/usr/bin/perl
use strict;
use warnings;

my %hash;
my ($key, $value);

while(<DATA>) {
chomp;
(undef, $key, $value)=( split /\t/ );
$hash{$key}=$value; # i think i may be doing something wrong
here...
}

foreach my $key(%hash) {
print "Key=> $key\tValue=> $value\n"; #tried many ways to
loop/print, best results, but still wrong
}

__DATA__
1 St. Helena Napa Valley
2 Santa Lucia Highlands Monterey Bay Area
3 Oakville Napa Valley
4 Howell Mountain Napa Valley
7 Russian River Valley Sonoma
8 Santa Barbara County South Central Coast
11 Oakville Napa Valley
13 Russian River Valley Sonoma
15 Alexander Valley Sonoma
16 Ojai Valley South Central Coast
20 Valpolicella Veneto
22 Chianti Tuscany
28 Asti Piedmont
33 Chianti Tuscany
34 Shenandoah Valley of California Sierra Foothills
36 Oakville Napa Valley
37 Green Valley Central Valley
38 Yamhill Valley Willamette Valley
42 Santa Barbara County South Central Coast
45 Rutherford Napa Valley
49 St. Emilion Bordeaux
50 St. Emilion Bordeaux
51 Haut-Medoc Bordeaux
52 St. Emilion Bordeaux
53 St. Emilion Bordeaux
54 St. Emilion Bordeaux
55 Haut-Medoc Bordeaux
56 Carneros Sonoma
57 Medoc Bordeaux

and this is the output for the above script (by the way, in the actual
file, the colums above *are* seperated with tabs....

Key=> Carneros Value=> Bordeaux
Key=> Sonoma Value=> Bordeaux
Key=> Green Valley Value=> Bordeaux
Key=> Central Valley Value=> Bordeaux
Key=> St. Emilion Value=> Bordeaux
Key=> Bordeaux Value=> Bordeaux
Key=> Santa Barbara County Value=> Bordeaux
Key=> South Central Coast Value=> Bordeaux
Key=> Haut-Medoc Value=> Bordeaux
Key=> Bordeaux Value=> Bordeaux
Key=> Yamhill Valley Value=> Bordeaux
Key=> Willamette Valley Value=> Bordeaux
Key=> Rutherford Value=> Bordeaux
Key=> Napa Valley Value=> Bordeaux
Key=> Oakville Value=> Bordeaux
Key=> Napa Valley Value=> Bordeaux
Key=> Howell Mountain Value=> Bordeaux
Key=> Napa Valley Value=> Bordeaux
Key=> Santa Lucia Highlands Value=> Bordeaux
Key=> Monterey Bay Area Value=> Bordeaux
Key=> St. Helena Value=> Bordeaux
Key=> Napa Valley Value=> Bordeaux
Key=> Shenandoah Valley of California Value=> Bordeaux
Key=> Sierra Foothills Value=> Bordeaux
Key=> Medoc Value=> Bordeaux
Key=> Bordeaux Value=> Bordeaux
Key=> Chianti Value=> Bordeaux
Key=> Tuscany Value=> Bordeaux
Key=> Valpolicella Value=> Bordeaux
Key=> Veneto Value=> Bordeaux
Key=> Asti Value=> Bordeaux
Key=> Piedmont Value=> Bordeaux
Key=> Russian River Valley Value=> Bordeaux
Key=> Sonoma Value=> Bordeaux
Key=> Alexander Valley Value=> Bordeaux
Key=> Sonoma Value=> Bordeaux
Key=> Ojai Valley Value=> Bordeaux
Key=> South Central Coast Value=> Bordeaux


as you can see, not only am i not getting a "value", but i get the same
whatever over and over (a variable initialization problem...?) i also
get some erronious results, such as the third from teh bottom,
"Sonoma", that's a "value" (region) and is in the third column, not a
"key" in the second (subregions).

I think that it's all there, code, data and output and i think i have
explained the question/problem (even commented the code poorly).
thanks for all of your helps as i learn to be more self sufficient
coding....
 
I

it_says_BALLS_on_your forehead

i am having a tough time getting values to print.... below is my code
with output to follow (it's wrong, i can't get the "values"... either i
am doing something wrong in the "print" loop or in acquiring the data,
both are a line or two, if you could please have a look... thanks :eek:)

#!/usr/bin/perl
use strict;
use warnings;

my %hash;
my ($key, $value);

while(<DATA>) {
chomp;
(undef, $key, $value)=( split /\t/ );
$hash{$key}=$value; # i think i may be doing something wrong
here...
}

foreach my $key(%hash) {
print "Key=> $key\tValue=> $value\n"; #tried many ways to
loop/print, best results, but still wrong
}

here, either use:

while ( my ($key, $value) = each %hash ) {
print "Key=> $key\tValue= $value\n";
}

OR (if you want to sort)

for my $key (sort keys %hash) {
print "Key=> $key\tValue= $hash{$key}\n";
}
 
B

Brian McCauley

i am having a tough time getting values to print.... below is my code
with output to follow (it's wrong, i can't get the "values"... either i
am doing something wrong in the "print" loop or in acquiring the data,
both are a line or two, if you could please have a look... thanks :eek:)

Someone else has pointed out your immediate problem, but you also have
another latent problem - you are suffering from premature declaration.
#!/usr/bin/perl
use strict;
use warnings;

my %hash;
my ($key, $value);

These variables need not be (and hence should not be) declared outside
loop.
while(<DATA>) {
chomp;
(undef, $key, $value)=( split /\t/ );

Here is where $key,$value should have been declared.

(undef,my( $key, $value))= split /\t/;

Or, on recent Perl, it will even DWIM if you say:

my (undef, $key, $value)= split /\t/;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,840
Latest member
BrendanG78

Latest Threads

Top