Lines Matching refs:country

26 have a file of city and country names, like this:
35 and you want to produce an output like this, with each country mentioned
36 once, and then an alphabetical list of the cities in that country:
42 The natural way to do this is to have a hash whose keys are country
43 names. Associated with each country name key is a list of the cities in
44 that country. Each time you read a line of input, split it into a country
46 country, and append the new city to the list. When you're done reading
86 We'll come back to this city-country problem later, after we've seen
290 file of city and country names.
296 4 my ($city, $country) = split /, /;
297 5 $table{$country} = [] unless exists $table{$country};
298 6 push @{$table{$country}}, $city;
301 8 for my $country (sort keys %table) {
302 9 print "$country: ";
303 10 my @cities = @{$table{$country}};
311 We're going to have a hash, C<%table>, whose keys are country names,
334 8 for my $country (sort keys %table) {
335 9 print "$country: ";
336 10 my @cities = @{$table{$country}};
343 is in line 10. C<$table{$country}> looks up the key C<$country> in the
345 that country. L<B<Use Rule 1>|/B<Use Rule 1>> says that we can recover
346 the array by saying C<@{$table{$country}}>. Line 10 is just like
351 C<{$table{$country}}>. The C<@> tells Perl to get the entire array.
360 4 my ($city, $country) = split /, /;
361 5 $table{$country} = [] unless exists $table{$country};
362 6 push @{$table{$country}}, $city;
365 Lines 2-4 acquire a city and country name. Line 5 looks to see if the
366 country is already present as a key in the hash. If it's not, the
372 C<$table{$country}> now holds a reference to the array of cities seen
373 in that country so far. Line 6 is exactly like
378 C<{$table{$country}}>. The L<C<push>|perlfunc/push ARRAY,LIST> adds a
386 4 my ($city, $country) = split /, /;
387 5 #### $table{$country} = [] unless exists $table{$country};
388 6 push @{$table{$country}}, $city;
391 If there's already an entry in C<%table> for the current C<$country>,
393 C<$table{$country}>, which is a reference to an array, and push C<$city>
394 into the array. But what does it do when C<$country> holds a key, say