From StrategyWiki, the video game walkthrough and strategy guide wiki
Jump to navigation Jump to search
(changed function split to p.split)
(some edits; need to focus on using frame.args instead of named parameters)
Line 4: Line 4:
local p = {}
local p = {}


function p.explode(div,str) -- credit: http://richard.warburton.it
function p.explode(frame)


   if (div=='') then return false end
   if (frame.args[1]=='') then return false end
   local pos,arr = 0,{}
   local pos,arr = 0,{}
   -- for each divider found
   -- for each divider found
Line 18: Line 18:
end
end


function p.find(div,str)
function p.find(frame)
   return string.find(str,div,pos,true)
   return string.find(frame.args[1],frame.args[2],frame.args[3],true)
end
end



Revision as of 21:50, 7 August 2014

Documentation for this module may be created at Module:Explode/Documentation

--[[
This module is used to explode a passed string into parts. First parameter is the divider string to divide the string at. The second parameter is the string.
]]--
local p = {}

function p.explode(frame)

  if (frame.args[1]=='') then return false end
  local pos,arr = 0,{}
  -- for each divider found
  for st,sp in function() return string.find(str,div,pos,true) end do
    table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
    pos = sp + 1 -- Jump past current divider
  end
  table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
  return arr

end

function p.find(frame)
  return string.find(frame.args[1],frame.args[2],frame.args[3],true)
end

-- Compatibility: Lua-5.1
function p.split(str, pat)
   local t = {}  -- NOTE: use {n = 0} in Lua-5.0
   local fpat = "(.-)" .. pat
   local last_end = 1
   local s, e, cap = str:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
     table.insert(t,cap)
      end
      last_end = e+1
      s, e, cap = str:find(fpat, last_end)
   end
   if last_end <= #str then
      cap = str:sub(last_end)
      table.insert(t, cap)
   end
   return t
end

return p