in reply to Question on reference
In addition to what diotilevi correctly said regarding autovivication, the sample code is a slightly ecclectic mix of 'old' and 'new' Perl in that it mixes the use of selected my'd variables with global vars.
Cleaned up to bring it in line with more modern usage and making it use strict; complient it would look something like this.
Note that the code within =pod/=cut lines is simply comments showing what code would be requireed were it not for autovivication.
#! perl -sw use strict; # Without the next %table referes to a global var which doesn't declar +ation. my %table; while (<>) { chomp; my ($city, $country) = split /, /; push @{$table{$country}}, $city; # The line above is equivalent to =pod if (not exists $title{$country}) { my @array; push @array, $city; $title{$country} = \@array; } else { my $arrayRef = $title{$country}; push @{$arrayRef}, $city; } =cut } foreach my $country (sort keys %table) { print "$country: "; my @cities = @{$table{$country}}; print join ',', sort @cities; print ".\n"; #The 3 lines above could be replaced by =pod print join( ',' @{$table{$country}} ), "\n"; =cut }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Offtopic: POD directives
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 | |
|
Re: Re: Question on reference
by kiat (Vicar) on Oct 27, 2002 at 02:46 UTC |