Module:Sandbox/Matroc/Ship prefix

Source: Wikipedia, the free encyclopedia.
-- Module:Ship prefix - testing module for existing template Ship prefix
-- which give the len of certain ship prefixes??
-- Created several tables in alphabetical order for easy viewing/editorial purposes
-- Joined all the tables into one for processing
-- Direct invoke: {{#invoke:Ship prefix|prefix|HMS Hood}}
-- Template:Ship prefix
-- {{#invoke:Ship prefix|prefix|{{{1|}}}}}<noinclude>
-- {{documentation}}
-- </noinclude>

local p = {}

function p.prefix(frame)

   local prefix_list = {}
   local t1 = {}
   local t2 = { 'CS', 'MS', 'MV', 'PS', 'RV', 'SM', 'SS' }
   local t3 = { 'ARA', 'ARC', 'CSS', 'GTS', 'HMS', 'HMT', 'HMC', 'INS', 'KDM', 'NRP', 'ORP', 'RMS', 'RFA', 'SMS', 'TCG', 'USS' }
   local t4 = { 'CCGS', 'CFAV', 'HDMS', 'HMAS', 'HMCS', 'HMVS', 'HMQS', 'HTMS', 'RMAS', 'USAS', 'USNS', 'USRC',  'RNLB', 'HMBS' }
   local t5 = { 'HNLMS', 'HMNZS', 'HSwMS', 'USAFS', 'USCGC' }
   local t6 = {}

   for k,v in ipairs(t1) do table.insert(prefix_list, v) end
   for k,v in ipairs(t2) do table.insert(prefix_list, v) end
   for k,v in ipairs(t3) do table.insert(prefix_list, v) end
   for k,v in ipairs(t4) do table.insert(prefix_list, v) end
   for k,v in ipairs(t5) do table.insert(prefix_list, v) end
   for k,v in ipairs(t6) do table.insert(prefix_list, v) end
  
   local prefix_len = 0
   local shipname = ""
   shipname = frame.args[1] or "--"
   if shipname == nil then
        prefix_len = 0
     elseif shipname == "--" then
        prefix_len = 0
     -- if shipname = "" prefix_len already set to 0
     else
        shipname = (shipname:gsub("(%s.*)",""))
        for i,v in pairs(prefix_list) do
          if shipname == v then
              prefix_len = string.len(v)
              break
            end -- END IF
        end -- END FOR
     end -- END IF

return prefix_len

end -- END FUNCTION prefix

return p