1. Array in hash
#!/usr/bin/perl -w use strict; use Data::Dumper; my (%all, $city, $country, $cities); while (<DATA>) { chomp; ($city, $country) = split /, /; # $all{$country} = [] unless exists $all{$country}; push @{$all{$country}}, $city; } for $country (sort keys %all) { print "$country: "; $cities = \@{$all{$country}}; my @order = sort @$cities; print $order[0], "\t"; for my $ID (sort @$cities) { print $ID, "\t"; } # print $table{$country}; print "\n"; } print Dumper (\%all); __DATA__ Chicago, USA Frankfurt, Germany Berlin, Germany Washington, USA Helsinki, Finland New York, USA
Output:
Finland: Helsinki Helsinki Germany: Berlin Berlin Frankfurt USA: Chicago Chicago New York Washington $VAR1 = { 'Finland' => [ 'Helsinki' ], 'Germany' => [ 'Frankfurt', 'Berlin' ], 'USA' => [ 'Chicago', 'Washington', 'New York' ] };
没有评论:
发表评论