1#! /usr/bin/perl -w 2 3use strict; 4use Geo::Postcodes::Update; 5 6################################################################################ 7# # 8# misc/update_no # 9# -------------- # 10# Arne Sommer - perl@bbop.org - 24. September 2006 # 11# # 12################################################################################ 13# # 14# This program converts the norwegian postcodes to a suitable data structure. # 15# # 16# !!! This is done on the library file 'NO.pm' itself !!! # 17# # 18################################################################################ 19# # 20# This program will show the new data on screen by default. # 21# Use the '-update' command line argument to update the module. # 22# # 23################################################################################ 24 25my $class = 'NO'; 26my $file = 'tilbud5'; 27my $url = 'http://epab.posten.no/Norsk/Nedlasting/_Files/tilbud5'; 28my $procedure = \&parse_no; 29 30my $update = @ARGV && $ARGV[0] eq "-update"; 31 32Geo::Postcodes::Update::update($class, $file, $url, $procedure, $update); 33 34################################################################################ 35 36sub parse_no 37{ 38 ## Step 1. Initialising data ################################################ 39 40 my %fix_type; $fix_type{B} = 'STBX'; # The keys (e.g. 'B') are used in the # 41 $fix_type{F} = 'MU'; # postcode file ('tilbud5'). # 42 $fix_type{G} = 'ST'; # The values (e.g. 'STBX') are the # 43 $fix_type{K} = 'IO'; # codes used by the Geo::Postcodes # 44 $fix_type{P} = 'BX'; # modules. # 45 $fix_type{S} = 'SX'; 46 47 my %location; # postcode > location # These hashes are # 48 my %borough_number; # postcode > borough number # used to collect # 49 my %borough; # borough number > borough # the postcode # 50 my %type; # postcode > type # data. # 51 52 my($postcode, $location, $borough_number, $borough, $type); 53 # Placeholders for the information in the postcode file. 54 55 my @out; # The procedure returns a list of perl code. They are stored here. # 56 57 ## Step 2. Build the internal data structure ################################ 58 59 foreach (@_) 60 { 61 # s/"/\\"/g; # Escape any double quotes used in the source file. 62 tr/�\235\217�/����/; # Fix curious coding of 8-bit characters in 'tilbud5'. 63 64 ($postcode, $location, $borough_number, $borough, $type) 65 = unpack("A4A32A4A30A", $_); 66 67 $location {$postcode} = $location; 68 $borough_number{$postcode} = $borough_number; 69 $borough {$borough_number} = $borough; 70 $type {$postcode} = $fix_type{$type}; 71 } 72 73 ## Step 3. Output the main data ############################################# 74 75 foreach (sort keys %location) 76 { 77 push(@out, "\$borough_number{'" . $_ . "'} = '" . $borough_number{$_} . "';" . 78 " \$type{'" . $_ . "'} = '" . $type{$_} . "';" . 79 " \$location{'" . $_ . "'} = '" . $location{$_} . "';\n"); 80 } 81 82 push(@out, "\n"); 83 84 ## Step 4. Output the borough mappings ###################################### 85 86 foreach (sort keys %borough) 87 { 88 push(@out, "\$borough{'" . $_ . "'} = '" . $borough{$_} . "';\n"); 89 } 90 91 ## Step 5. Return the lot ################################################### 92 93 return (@out, "\n"); 94} 95