From StrategyWiki, the video game walkthrough and strategy guide wiki
Jump to navigation Jump to search
(creation, first LUA test module)
 
(added local p)
Line 2: Line 2:
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.
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 explode(div,str) -- credit: http://richard.warburton.it
function explode(div,str) -- credit: http://richard.warburton.it
   if (div=='') then return false end
   if (div=='') then return false end
Line 13: Line 14:
   return arr
   return arr
end
end
return p

Revision as of 00:54, 27 July 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 explode(div,str) -- credit: http://richard.warburton.it
  if (div=='') 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

return p