[Asterisk-Dev] Algorithm to parse country code

Mike Tkachuk mike at yes.net.ua
Mon May 10 08:25:20 MST 2004


Hello Terence,

Monday, May 10, 2004, 6:00:24 PM, you wrote:


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

DOnt' clearly understood what you want, but
maybe you need this?
If you have table like:
route   country
1       USA
1212    USA-NY
49      Germany

and you have called number like:
12121234567

than to select row 1212 USA-NY you can use this query:

select * from table where route in
('1','12','121','1212','12121','121212','1212123','12121234','121212345','1212123456','12121234567')

this code will use indexes in search... so it fast
or you  can use smth like this (it's more easy)
select * from routes where '0012121234567' like concat(route,'%')
-- 
Best regards,

~*-,._.,-*~'`^`'~*-,._.,-*~'`^`'~*-,.
Mike Tkachuk,     ph:380-3433-47067
YES ISP,          fx:380-3433-47067
Valova 17,        mike|a|yes.net.ua  
Kolomyia,         www.yes.net.ua
Ukraine 78200     FWD: 66518 

            10.05.2004 
          ICQ# 57698805
   MSN: mike_tkachuk|a|hotmail.com
~*-,._.,-*~'`^`'~*-,._.,-*~'`^`'~*-,.




More information about the asterisk-dev mailing list