I need to do a simple split of a string, but there doesnt seem to be a function for this, and the manual way i tested didn't seem to work. How would i do it?
Please see Splitting Strings:
Here are various ways of splitting a string into a list of substrings, breaking the original string on occurrences of some separator (character, character set, or pattern). This is commonly called a string split[2] function.
If you are splitting a string in Lua, you should try the string.gmatch() or string.sub() methods. Use the string.sub() method if you know the index you wish to split the string at, or use the string.gmatch() if you will parse the string to find the location to split the string at.
Example using string.gmatch() from Lua 5.1 Reference Manual:
t = {}
s = "from=world, to=Lua"
for k, v in string.gmatch(s, "(%w+)=(%w+)") do
t[k] = v
end
Here is The Function
====================================================
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
===============================================
Call it like,
list=split(string-to-split,pattern-to-match)
e.g.
list=split("1:2:3:4","\:")
===============================================
For more go here,
http://lua-users.org/wiki/SplitJoin
Just as string.gmatch
will find patterns in a string, this function will find the things between patterns:
function string:split(pat)
pat = pat or '%s+'
local st, g = 1, self:gmatch("()("..pat..")")
local function getter(segs, seps, sep, cap1, ...)
st = sep and seps + #sep
return self:sub(segs, (seps or 0) - 1), cap1 or sep, ...
end
return function() if st then return getter(st, g()) end end
end
By default it returns whatever is separated by whitespace.
im having an issue in lua for psp...
Im sending a string over ADHOC mode and i cant split the string on the other side...
script A will send the string like so Adhoc.send("hell0 /n date:")
And script B would spilt the string but idk how...
my friend said this:
data = Adhoc.recv()
lines = split(data) --you need to write the 'split' function yourself
screen:print(0, 0, data[1], green) --"hello" screen:print(0, 0, data[2], green) --"7/09/10"
so like how would i split this ????
I would avoid Lua. The documentation is non-existent.
Lots of responders pointed to:
http://lua-users.org/wiki/SplitJoin
but of course, nothing on that page actually works in Lua:
Lua 5.1.2 Copyright (C) 1994-2007 Lua.org, PUC-Rio > split("123", "") stdin:1: attempt to call global 'split' (a nil value) stack traceback: stdin:1: in main chunk [C]: ? >
Looking at the non-searchable content at lua.org doesn't turn up much on the standard library (I assume there is one).
If something as simple as splitting a string can't be adequately documented, well, I wouldn't expect anything complicated to be achievable without a lot of pain.
If you just want to iterate over the tokens, this is pretty neat:
line = "one, two and 3!"
for token in string.gmatch(line, "[^%s]+") do
print(token)
end
Output:
one,
two
and
3!
Short explanation: the "[^%s]+" pattern matches to every non-empty string in between space characters.