Module:ISO 3166/data
local p = {}
 
 local data = mw.loadData(&quot;Module:ISO 3166/data/National&quot;)
 
 function p.list(frame)
 --Returns a list of subpages and associated countries
 local codes = {}
 for alpha2,cdata in pairs(data) do
   codes[#codes+1] = {alpha2,cdata.name}
 end
 table.sort(codes, function(t1,t2) return t1[1]&lt;t2[1] end)
 
 local out = {&quot;* [[Module:ISO 3166/data/National|National]]&quot;}
 for _,cdata in ipairs(codes) do
   out[#out+1] = &quot;* [[Module:ISO 3166/data/&quot;..cdata[1]..&quot;|&quot;..cdata[1]..&quot; (&quot;..cdata[2]..&quot;)]]&quot;
 end
 return table.concat(out,&quot;\n&quot;)
 
 end
 
 function p.subpagetest(frame)
 --Returns a table row indicating the existence of data pages (see [[User:SiBr4/sandbox#Data subpage test]])
 
 local function cell(code,d)
   local sdata
   if not data[code] then
     return &quot;&quot; --Unassigned or reserved 3166-1 code
   else
     local sname = &quot;Module:ISO 3166/data/&quot;..code
     if not (d or mw.title.new(sname).exists) then
       return &quot;title=\&quot;&quot;..data[code].name..&quot;\&quot; style=\&quot;background:#fee;\&quot;|[[&quot;..sname..&quot;|-]]&quot; --Data subpage doesn't exist
     else
       sdata = mw.loadData(sname)
     end
   end
   for _,_ in pairs(sdata) do
     return &quot;title=\&quot;&quot;..data[code].name..&quot;\&quot; style=\&quot;background:#efe;\&quot;|n&quot; --Non-empty 3166-2 entry
   end
   return &quot;title=\&quot;&quot;..data[code].name..&quot;\&quot; style=\&quot;background:#eef;\&quot;|e&quot; --Empty 3166-2 entry
 end
 
 local letter = frame.args[1]
 local out = &quot;|-\n!&quot;..letter..&quot;*&quot;
 for i = 65, 90 do
   out = out..&quot;\n|&quot;..cell(letter..string.char(i),frame.args[2])
 end
 return out
 
 end
 
 function p.testall(frame)
 --Search across all subpages for problems with names
 
 local strip = require(&quot;Module:ISO 3166&quot;).strip
 
 local function subpage(p)
   return &quot;[[Module:ISO 3166/data/&quot;..p..&quot;|/&quot;..p..&quot;]]&quot;
 end
 local function testname(names,inames,dnames,page,code,name)
   if name then
   	if type(name)~=&quot;string&quot; then
   	  dnames[#dnames+1] = &quot;*Unexpected non-string value in &quot;..subpage(page)..&quot; (&quot;..code..&quot;)&quot;
   	else
       local sname = strip(name)
       if not names[sname] then
         inames[sname] = name
       else
         dnames[#dnames+1] = &quot;*Duplicate name: \&quot;&quot;..name..&quot;\&quot; in &quot;..subpage(page)
                             ..(names[sname]==name and &quot; (exact)&quot; or &quot; (non-exact)&quot;)..&quot; (&quot;..code..&quot;)&quot;
       end
     end
   end
   return names, inames, dnames
 end
 local function testnametable(names,inames,dnames,page,code,tab)
   if tab then
     if type(tab)~=&quot;table&quot; then
       dnames[#dnames+1] = &quot;*Unexpected non-table value in &quot;..subpage(page)..&quot; (&quot;..code..&quot;)&quot;
     else
       for _,name in pairs(tab) do
         names, inames, dnames = testname(names,inames,dnames,page,code,name)
       end
     end
   end
   return names, inames, dnames
 end
 local fields = {name=1,isoname=1,isonames=1,displayname=1,isodisplayname=1,isodisplaynames=1,altnames=1,lang=1,defaultlang=1,alpha3=1,numeric=1,nocode=1}
 local function testentry(names,dnames,page,code,edata)
   for k,_ in pairs(edata) do
     if not fields[k] then
       dnames[#dnames+1] = &quot;*Unknown field \&quot;&quot;..k..&quot;\&quot; in &quot;..subpage(page)..&quot; (&quot;..code..&quot;)&quot;
     end
   end
   local inames = {}
   names, inames, dnames = testname(names,inames,dnames,page,code,edata.name)
   names, inames, dnames = testname(names,inames,dnames,page,code,edata.isoname)
   names, inames, dnames = testnametable(names,inames,dnames,page,code,edata.isonames)
   names, inames, dnames = testnametable(names,inames,dnames,page,code,edata.altnames)
   if not edata.nocode then
     names, inames, dnames = testname(names,inames,dnames,page,code,code)
   end
   if page==&quot;National&quot; then
     names, inames, dnames = testname(names,inames,dnames,page,code,edata.alpha3)
     names, inames, dnames = testname(names,inames,dnames,page,code,edata.numeric)
   end
   for k,v in pairs(inames) do names[k]=v end
   return names, dnames
 end
 
 local dnames = {}
 
 --ISO 3166-1
 local names = {}
 for code,cdata in pairs(data) do
   names, dnames = testentry(names,dnames,&quot;National&quot;,code,cdata)
 end
 
 --ISO 3166-2
 for code,_ in pairs(data) do
   names = {}
   local spage = &quot;Module:ISO 3166/data/&quot;..code
   if mw.title.new(spage).exists then
     local sdata = mw.loadData(spage)
     for scode,cdata in pairs(sdata) do
       if type(cdata)==&quot;table&quot; then
         names, dnames = testentry(names,dnames,code,scode,cdata)
       end
     end
   end
 end
 
 if #dnames&gt;0 then
   return table.concat(dnames,&quot;\n&quot;)
 else
   return &quot;None&quot;
 end
 
 end