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 }

Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!

Replies are listed 'Best First'.
Offtopic: POD directives
by Aristotle (Chancellor) on Oct 26, 2002 at 16:43 UTC
    You have to put POD directives in a paragraph of their own, ie preceeded and followed by a blank line.

    Makeshifts last the longest.

      No you don't.

      Not if you are only using them as a convenient form of multi line comment. I seriously doubt that anyone is going to run perldoc on this snippet.


      Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy
        Yes you do. If you're going to bother using pod, use pod as it is defined, not according to quirks in the perl implementation, cause I'll bet you that by perl 5.9 you won't be able to rely on it to work.

        update: Thanks man: "You probably shouldn't rely upon the warn() being podded out forever. "

        update: rir: `perldoc perlpod'. As for things ludicrous pod is not secret stuff.

        update: I ran accross Re^2: Crash Course in POD again, a reply to one of my nodes , and it lists a trick by Larry himself that I like very much ;)(one of the most basic features of pod)

        =for Later for a single paragraph =cut =begin for later Stuff for later =end for later print 'h1'; =begin forlater print 'bye'; =end forlater

        ____________________________________________________
        ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: Re: Question on reference
by kiat (Vicar) on Oct 27, 2002 at 02:46 UTC
    Thanks, BrowserUk! Your expanded code is a great help!