lightoverhead has asked for the wisdom of the Perl Monks concerning the following question:
then test the code as below:Chicago, USA Frankfurt, Germany Berlin, Germany Washington, USA Helsinki, Finland New York, USA
Wierd thing happend. first, I can not print the element of $table{"USA"}, giving the error "Use of uninitialized value in print at country_city.pl line 12, <> line 6.". Second, the foreach loop can not find $country equals to "USA", thus, it can not print "USA is found", it just printed nothing. What happened? This really confused me. Thanks.#!/usr/bin/perl -w use strict; my %table; while (<>){ chomp; my ($city,$country)=split/,/; $table{$country}=[] unless exists $table{$country}; push @{$table{$country}}, $city; } my $a=$table{"USA"}; print $a; #line 12 foreach my $country (sort keys %table){ if ($country eq "USA"){ print "USA is found\n"; } else{} }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Can not print hash element
by dreadpiratepeter (Priest) on Sep 23, 2008 at 01:49 UTC | |
by lightoverhead (Pilgrim) on Sep 23, 2008 at 02:05 UTC | |
by tinita (Parson) on Sep 23, 2008 at 18:24 UTC | |
|
Re: Can not print hash element
by lamp (Chaplain) on Sep 23, 2008 at 02:05 UTC | |
|
Re: Can not print hash element
by poolpi (Hermit) on Sep 23, 2008 at 10:43 UTC |