In my program, the user can enter a text. That text will have three parts, eg. "asd meme qwerty". How can i separate each part and store them into different variables? The spaces will of course have to be left out.
[Lua][Question] Parsing user input into three parts
Started by Backplague, Jun 20 2012 10:07 PM
5 replies to this topic
#1
Posted 20 June 2012 - 10:07 PM
#2
Posted 20 June 2012 - 10:19 PM
You could do this... but that would involve... just look
local msg = {}
msg.part1 = io.read()
msg.part2 = io.read()
msg.part3 = io.read()
-- and so on if you need more than 3 parts
textutils.serialize(msg)
The textutils.serialize(msg) combines everything. When you want to uncombine everything do this...textutils.unserialize(msg) -- Now you have variables part1, part2, and so on
#3
Posted 20 June 2012 - 10:28 PM
You can split the user input by creating your own split function. Here is one I made that splits where it finds a comma, you can modify it rather easily to split at the spaces.
I would modify it but I have to go to work now and wont be able to test it. Figured I would give you something to play with.
You will want to capture the output of the function in a table so..
function pattern(text,pattern,start)
return string.sub(text,string.find(text,pattern,start)) end
function split(string)
local sep = {}
local done = false
local count,prevVars,tmp = 0,0,0
string = string..",|"
while not done do
count = count + 1
tmp = pattern(string,"[^,]+",count+prevVars)
if tmp == "|" then done = true return sep end
prevVars = prevVars + tmp:len()
table.insert(sep,count,tmp)
end
return sep
end
I would modify it but I have to go to work now and wont be able to test it. Figured I would give you something to play with.
You will want to capture the output of the function in a table so..
local tOutput = {}
write ("Enter Input")
local input = read()
tOutput = split(input)
#4
Posted 20 June 2012 - 10:37 PM
luanub, on 20 June 2012 - 10:28 PM, said:
You can split the user input by creating your own split function. Here is one I made that splits where it finds a comma, you can modify it rather easily to split at the spaces.
I would modify it but I have to go to work now and wont be able to test it. Figured I would give you something to play with.
You will want to capture the output of the function in a table so..
function pattern(text,pattern,start)
return string.sub(text,string.find(text,pattern,start)) end
function split(string)
local sep = {}
local done = false
local count,prevVars,tmp = 0,0,0
string = string..",|"
while not done do
count = count + 1
tmp = pattern(string,"[^,]+",count+prevVars)
if tmp == "|" then done = true return sep end
prevVars = prevVars + tmp:len()
table.insert(sep,count,tmp)
end
return sep
end
I would modify it but I have to go to work now and wont be able to test it. Figured I would give you something to play with.
You will want to capture the output of the function in a table so..
local tOutput = {}
write ("Enter Input")
local input = read()
tOutput = split(input)
How can i swap the ',' to a space? I cant really understand the pattern function.
#5
Posted 20 June 2012 - 10:55 PM
You can use this:
local tWords = {}
for match in string.gmatch(userInput, "[^ t]+") do
table.insert( tWords, match )
end
it would separate the words and put them in a table, then you can access them like:print(tWords[1], ", ", tWords[2])And it works for any number of words.
#6
Posted 20 June 2012 - 10:58 PM
MysticT, on 20 June 2012 - 10:55 PM, said:
You can use this:
local tWords = {}
for match in string.gmatch(userInput, "[^ t]+") do
table.insert( tWords, match )
end
it would separate the words and put them in a table, then you can access them like:print(tWords[1], ", ", tWords[2])And it works for any number of words.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











