Hi, what you need is the longest prefix match from a list of variable length strings. One possible approach would be to create a hash of hashes of hashes... where at each level one digit of the given number is a key
identifies an edge (of a labeled 10-ary tree?).
Another idea would be to look-up the prefixes from a simple hash starting with the longest substring from the phone number to check.
Something like this (too tired to clean it up, but it shall convey an idea):
#!/bin/perl use strict; use warnings; my %tab; foreach (<DATA>) { chomp; $tab{$1}=$2 if /^\s*0(\d+)\s+(.*)/; # e.g. 30 -> Berlin # you can compute min/max of the prefix here (s. below) } # already normalised w/o leading 0 NUMBER: foreach my $num ( qw(204112345 3304123456 3145666 301234567) ) + { for (my $len=5; $len>=2; $len--) { # assumes max. prefix lenght is 5, min. is 2 my $prefix = substr($num,0,$len); if (defined (my $town=$tab{$prefix})) { printf "0%-12s = %5s-%-7s in sunny %s\n", $num, "0$prefix", substr($num,$len), $town; next NUMBER; } } print "NO MATCH FOR: 0$num\n"; } __DATA__ 0201 Essen, Ruhr 0202 Wuppertal 0203 Duisburg 02041 Bottrop 02043 Gladbeck Westf 0208 Muelheim a.d. Ruhr 0208 Oberhausen Rheinl 212 Solingen 02129 Haan Rheinl 030 Berlin 03301 Oranienburg 03302 Hennigsdorf 03303 Birkenwerder 03304 Velten 033051 Nassenheide 033053 Zehlendorf Kr Oberhav 033054 Liebenwalde 033055 Kremmen 033056 Muehlenbeck Kr Oberhav 03306 Gransee 03307 Zehdenick
This prints:
Well, 'sunny' is just wishful thinking... Update:0204112345 = 02041-12345 in sunny Bottrop 03304123456 = 03304-123456 in sunny Velten NO MATCH FOR: 03145666 0301234567 = 030-1234567 in sunny Berlin
In reply to Re: (german) region code detection - request for thoughts
by Perlbotics
in thread (german) region code detection - request for thoughts
by Skeeve
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |