This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
OniNoSeishin's profile picture

[lua] string split

Started by OniNoSeishin, 26 March 2012 - 01:12 PM
OniNoSeishin #1
Posted 26 March 2012 - 03:12 PM
Does anyone have a working string split function? 'cause i'm not able doing one myself and i can't find one working on the web.

first one i used:
Spoiler

function separate(string, divider)
    local function find(string, match, startIndex)
        if not match then return nil end
        _ = startIndex or 1
        local _s = nil
        local _e = nil
        _len = match:len()
        while true do
            _t = string:sub( _ , _len + _ - 1)
            if _t == match then
                _s = _
                _e = _ + _len - 1
                break
            end
            _ = _ + 1
            if _ > string:len() then break end
        end
        if _s == nil then return nil else return _s, _e end
    end

    if not divider then return nil end
    local start = {}
    local endS = {}
    local n=1
    repeat
        if n==1 then
            start[n], endS[n] = find(string, divider)
        else
            start[n], endS[n] = find(string, divider, endS[n-1]+1)
        end
        n=n+1
    until start[n-1]==nil
    local subs = {}
    for n=1, #start+1 do
        if n==1 then
            subs[n] = string:sub(1, start[n]-1)
        elseif n==#start+1 then
            subs[n] = string:sub(endS[n-1]+1)
        else
            subs[n] = string:sub(endS[n-1]+1, start[n]-1)
        end
    end
    return subs
end
problem: throw an error if the string doesn't have at least 1 delimiter -> i stopped using it

second one i used:
Spoiler

function split(string, delimiter)
  local result = { }
  local from = 1
  local delim_from, delim_to = string.find( string, delimiter, from )
  while delim_from do
    table.insert( result, string.sub( string, from , delim_from-1 ) )
    from = delim_to + 1
    delim_from, delim_to = string.find( string, delimiter, from )
  end
  table.insert( result, string.sub( string, from ) )
  return result
end
problem: was working good, but then i had a problem with this test-script:
Spoiler

str2 = "0.1 1.2|0.3.test3 1.5.test5"

sep = split(str2, "|")
print("\nSEP 1: "..sep[1])
print("SEP 2: "..sep[2].."\n")
spl1 = split(sep[1], " ")
spl2 = split(sep[2], " ")

print("original: "..str2.."\nsplitted sep 1:")
for x, y in pairs(spl1) do
    print(x.." "..y)
end
print("splitted sep 2:")
for x,y in pairs(spl2) do
    print(x.." "..y)
end
problem showed by this script: sep2 is splitted bad…
Advert #2
Posted 26 March 2012 - 03:49 PM
I find it hard to believe you haven't found a working one.

http://lua-users.org/wiki/SplitJoin

There are multiple working variations there, suited to many different needs.

It'd help if you can show us what you expect the split function to do, and the results of running your code so we don't have to run it ourselves.
OniNoSeishin #3
Posted 26 March 2012 - 04:24 PM
i already saw that wiki page, but i didn't tested much of them because, i've to admit this, i'm damn lazy and i hate lua doesn't have a built-in split function. Sorry for this…

i think i found the one which suites my situation, so i'm ok for now.

Thanks

EDIT:
nope, can't find myself one which is ok for me. Some use "string.gfind" which result in an attempt to call nil, others have problems with points as delimiter, others have problems if there are no delimiters in original string.

Here's an example of what i need:

original string is "a.b c.d|e.f.g h.i.j"

in the first split i use "|" as delimiter, obtaining "a.b c.d" and "e.f.g h.i.j"
in the second split i use " " (space) as delimiter, obtaining "a.b" and "c.d" (in case the string is "a.b c.d")
in the third one i use "." (point) as delimiter, obtaining "a" and "b" (in case the string is "a.b")
Advert #4
Posted 26 March 2012 - 05:14 PM
You can replace the string.gfind calls with string.gmatch, I believe they are identical functions (gfind was renamed to gmatch, but LuaJ doesn't provide compatability).
LeCanardNoir #5
Posted 05 March 2013 - 03:06 PM
i use this one


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


http://stackoverflow.com/questions/1426954/split-string-in-lua
Cranium #6
Posted 05 March 2013 - 03:15 PM
Enough with the necro. Locked,