lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>  But only country "1" has 1 digit...

Not true!  Russia has a single digit code too!  Russia shares it's
digit - 7 - with Kazakhstan, while the US shares it's 1 with Canada
and seventeen other countries not counting US territories [1].

>  for c,a in ipairs(country) do
>         if string.match(s,"^"..c) then
>                 for aa in ipairs(a) do
>                         local n=string.match(s,"^"..c..aa.."(%d+)$")
>                         if n ~= nil then found(n) break ebd
>                 end
>         end
>  end

It may make sense to exploit the structure in the system:

    -- pairs of digits that start three digit codes
    three_digits = {"21", "22", "23", "24", "25", "26", "29", "35",
"37", "38", "42",
          "50", "59", "67", "68", "69", "80", "85", "87", "88", "96",
"97", "99"}

    function country_code(s)
        local first = s:sub(1,1)
        if first == "1" or first == "7" then
           return first, s:sub(2) -- a one digit code
        else
           local first_two = s:sub(1,2)
           for _, prefix in ipairs(three_digits) do
              if first_two == prefix then
                 return s:sub(1,3), s:sub(4) -- a three digit code
              end
           end
           return first_two, s:sub(3) -- a two digit code
        end
    end

  - yuri

[1]: http://en.wikipedia.org/wiki/NANPA

-- 
http://sputnik.freewisdom.org/