lua-users home
lua-l archive

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


Since we have table.concat, any chance of adding a complementing function (perhaps table.split) to the standard libraries?

If I knew C, I'd translate my Lua code below, but this is what I've been using:

function table.split(str,div)
   local pos,tab = 0, {}
for st, sp in function() return string.find(str,div,pos,true) end do -- for each divider found table.insert(tab,string.sub(str,pos,st-1)) -- Attach chars left of current divider
       pos = sp + 1 -- Jump past current divider
   end
table.insert(tab,string.sub(str,pos)) -- Attach chars right of last divider
   return tab
end

Admittedly, this might be more useful to CGILua programmers (equivalent to PHP's explode) than your average scripters.