kiat has asked for the wisdom of the Perl Monks concerning the following question:
While I understand more or less what's going on, I don't particularly understand line 4, despite the author's explanation. How is %table created? I thought that for a hash to be created, a value must be assigned to the key, as in:# Assume the following input delimited be a newline Chicago, USA Frankfurt, Germany Berlin, Germany Washington, USA Helsinki, Finland New York, USA 1 while (<>) { 2 chomp; 3 my ($city, $country) = split /, /; 4 push @{$table{$country}}, $city; 5 } 6 7 foreach $country (sort keys %table) { 8 print "$country: "; 9 my @cities = @{$table{$country}}; 10 print join ’, ’, sort @cities; 11 print ".\n"; 12 } In the first part, line 4 is the important one. We’re going to have a + hash, %table, whose keys are country names, and whose values are (re +ferences to) arrays of city names. After acquiring a city and countr +y name, the program looks up $table{$country}, which holds (a referen +ce to) the list of cities seen in that country so far.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Question on reference
by BrowserUk (Patriarch) on Oct 26, 2002 at 15:37 UTC | |
by Aristotle (Chancellor) on Oct 26, 2002 at 16:43 UTC | |
by BrowserUk (Patriarch) on Oct 26, 2002 at 17:14 UTC | |
by PodMaster (Abbot) on Oct 27, 2002 at 09:22 UTC | |
by BrowserUk (Patriarch) on Oct 27, 2002 at 09:33 UTC | |
by rir (Vicar) on Oct 27, 2002 at 19:58 UTC | |
by kiat (Vicar) on Oct 27, 2002 at 02:46 UTC | |
|
Re: Question on reference
by diotalevi (Canon) on Oct 26, 2002 at 14:48 UTC |