Hi,

Following a discussion on talk-gb about ways of formatting a phone number I decided to see how far I could get with the current style functions in mkgmap. I was very pleasantly surprised! The recent additions of subst with a regex and the part function have allowed me to get an almost perfect result with the countries I have looked at. I thought I would share my experiences in case others also find it useful.

The goal is to normalise all POI phone numbers to international format so starting with a + followed by the country code etc. without any punctuation or extraneous information. I put the following code in "inc/phone" and used the include function to invoke that from "inc/address", instead of the existing two lines which derive mkgmap:phone.

========== start of script

# reformat phone numbers

# get the number to use
phone=* | contact:phone=* | addr:phone=* {set cs:phone='${phone}' | '${contact:phone}' | '${addr:phone}'}

# only use the first number if there is a list
cs:phone=* {set cs:phone='${cs:phone|part:}'}

# country-specific rules
cs:phone=* & (mkgmap:country=GBR | mkgmap:country=IMN)
{set cs:phone='${cs:phone|subst:(0)=>|subst:^00~>+|subst:[()]~>|subst:^0~>+44|subst:^\+440~>+44}'}
cs:phone=* & mkgmap:country=LUX
{set cs:phone='${cs:phone|subst:^00~>+}'}
cs:phone=* & mkgmap:country=IRL
{set cs:phone='${cs:phone|subst:^00~>+|subst:[- ()]~>|subst:^0~>+353|subst:^\+3530~>+353}'}
cs:phone=* & mkgmap:country=NLD
{set cs:phone='${cs:phone|subst:^00~>+|subst:[- ()]~>|subst:^0~>+31|subst:^\+310~>+31}'}
cs:phone=* & mkgmap:country=BEL
{set cs:phone='${cs:phone|subst:^00~>+|subst:[- ()]~>|subst:^0~>+32|subst:^\+320~>+32}'}
cs:phone=* & mkgmap:country=FRA
{set cs:phone='${cs:phone|subst:^00~>+|subst:^0~>+33}'}
cs:phone=* & mkgmap:country=DEU
{set cs:phone='${cs:phone|subst:^00~>+|subst:[- ()]~>|subst:^0~>+49|subst:^\+490~>+49}'}

# lose all non-digits (except for +)
cs:phone=* {set cs:phone='${cs:phone|subst:[^0-9+]~>}'}

# wrap up
cs:phone=* {echo '(${mkgmap:country}) Phone number ${phone} reformatted to ${cs:phone}' }
cs:phone=* {add mkgmap:phone='${cs:phone}'; set phone='${cs:phone}'; delete contact:phone; delete addr:phone;}

========== end of script

 A couple of things I missed:

1) the ability to do variable substitution within the old/new parameters to subst, e.g. subst:${old}~>${new}

2) the ability to use backreferences to parts of the pattern, e.g. subst:(abc)def~>\{1} to delete occurrences of "def" when they are preceded by "abc"

If these functions were to exist, many of the country-specific stuff could actually be collapsed into one or two rules, with variables for the country code and the trunk prefix (usually a 0 in Europe).

Best regards,

Colin