[Asterisk-Dev] Algorithm to parse country code

Rob Gagnon rob at networkip.net
Mon May 10 10:22:42 MST 2004


There is a subroutine for it as rules exist for how country codes are laid
out...

Let me find the one I wrote

and you dont need a database or config to do it



----- Original Message ----- 
From: "Robert Bedell" <robert.bedell at zivVa.com>
To: <asterisk-dev at lists.digium.com>
Sent: Monday, May 10, 2004 11:15 AM
Subject: RE: [Asterisk-Dev] Algorithm to parse country code


> Hey all,
>
> TP> I am trying to parse a dialed extension in order to deduce the country
> TP> code - but can't seem to find any references on efficient ways to do
> TP> this... though surely there is a well known good algorithm for this
> TP> already?
>
> TP> I plan to store my country details in a MySQL database. Conceptually,
> TP> i've already thought that I could successfully match a country from
the
> TP> dialled extension by reading the countries into an array (or b-tree or
> TP> something), and then going through the list several times starting
with
> TP> the four digit codes, then three digit ones, then two, then one etc...
> TP> until a match is made. I assume that country codes do not overlap in a
> TP> way which would make this not possible.
>
> TP> Is there a better way to do this? Even if I read all entries and store
> TP> them in a tree, I still have to potentially go through the tree four
> TP> times to reach the single digit country codes (most notably
USA/Canada)
> TP> - and every call of the PHP script would search the SQL
> TP> database/re-populate the tree... surely this would be very expensive
> TP> computationally?
>
> TP> I don't recall a feature in MySQL that allows you to query x number of
> TP> characters of a given field only.
>
> TP> Anyone have any better ideas?
>
> TP> Thanks
>
> TP> - Terence
>
> MT> DOnt' clearly understood what you want, but maybe you need this?
>
> MT> If you have table like:
>
> Call this "routes"
> MT> route   country
> MT> 1       USA
> MT> 1212    USA-NY
> MT> 49      Germany
>
> MT> and you have called number like:
> MT> 12121234567
>
> We do something similar here.  Why not use substring on the entire dialed
> extension?  If the 'route' is unique in the table above, the first row of
> the following query will give you the longest possible match on a given
> extension:
>
>   select *
>     from routes
>    where route=substring('12121234567',0,char_length(route))
>    order by length(route) desc
>
> Cheers!
>
> Robert
>
> _______________________________________________
> Asterisk-Dev mailing list
> Asterisk-Dev at lists.digium.com
> http://lists.digium.com/mailman/listinfo/asterisk-dev
> To UNSUBSCRIBE or update options visit:
>    http://lists.digium.com/mailman/listinfo/asterisk-dev
>




More information about the asterisk-dev mailing list