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:

0204112345 = 02041-12345 in sunny Bottrop 03304123456 = 03304-123456 in sunny Velten NO MATCH FOR: 03145666 0301234567 = 030-1234567 in sunny Berlin
Well, 'sunny' is just wishful thinking...

Update: removed, added

In reply to Re: (german) region code detection - request for thoughts by Perlbotics
in thread (german) region code detection - request for thoughts by Skeeve

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.