Chapter 10: Overlaying Statistical Data
#!/usr/bin/perl -w
use strict;
open(DATA,$ARGV[0]) or die “Couldn’t open file; did you forget it?”;
my $counter = 0;
print “
my @popyears;
while()
{
next unless (m/[a-z]/i);
$_ =~ s/\”//g;
$counter++;
if ($counter == 1)
{
my ($city,$lat,$lng);
($city,$lat,$lng,@popyears) = split /\t/;
$counter++;
next;
}
chomp;
my ($city,$lat,$lng,@pop) = split /\t/;
printf(‘<city title=”%s” lat=”%f” lng=”%f”>’,$city,$lat,$lng);
print(“\n”);
for(my $i=0;$i < scalar(@pop);$i++)
{
$pop[$i] =~ s/,//g;
next unless (defined($pop[$i]));
printf(‘<pop year=”%s” value=”%d”></pop>’,$popyears[$i],$pop[$i]);
print(“\n”);
}
print(“\n”);
}
close(DATA);
print “”;
The script is comparatively straightforward. The data is extracted and a little cleaning up is
performed (you remove double quotes from all lines and commas from population numbers)
before generating a suitable XML file. Following is a sample of the generated file: