Forum

> > CS2D > Scripts > LUA Split Function
Forums overviewCS2D overview Scripts overviewLog in to reply

English LUA Split Function

4 replies
To the start Previous 1 Next To the start

old LUA Split Function

sixpack OP
User Off Offline

Quote
Hi guys! I managed to save a file like this:
settings.txt
1
0 0 1 45 11 16000 5

These are several settings that the script will use when server starts.
I need you to tell me how to use the split function to load all this stuff like:
if(file exists) then
     var1 = 1st part of splitted line -0-
     var2 = 2nd part of splitted line -0-
     var3 = 3rd part of splitted line -1-
     var4 = 4th part of splitted line -45-
end

and so on.

old Re: LUA Split Function

Flacko
User Off Offline

Quote
Read the whole line and pass it to string.gmatch()
1
2
3
4
5
6
local t = {}
for w in string.gmatch(yourline, "%w+") do
	table.insert(t,w)
end
--Then use sth like
parse("setmoney "..id.." "..t[6])

old Reply

sixpack OP
User Off Offline

Quote
This doesn't work, there's an error that makes each variable in the file nil!

Any other suggestions using the split function?

old Re: LUA Split Function

Yasday
User Off Offline

Quote
You can use the toTable function, it's very easy.
1
2
3
4
5
6
7
8
9
function toTable(t,m)
	if not m then m = "%s"
	local tbl = {}
	m = "[^"..m.."]+"
	for w in string.gmatch(t,m) do
		table.insert(tbl,w)
	end
	return tbl
end
Now use it like this:
1
2
3
4
5
6
for lines in io.lines("settings.txt") do
	local line = toTable(lines)
	var1 = tonumber(line[1])
	var2 = tonumber(line[2])
	...
end
edited 2×, last 29.12.10 06:12:41 pm

old Reply

sixpack OP
User Off Offline

Quote
I'll try this and let you know soon...
I am making a very good script and this is the only error I get in theese 2.000 lines of code!

That really worked, thanks!

//Admin, Mods, close this thread!
edited 4×, last 29.12.10 07:04:12 pm
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview