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

[Lua][Question] Parsing user input into three parts

Started by Backplague, 20 June 2012 - 09:07 PM
Backplague #1
Posted 20 June 2012 - 11:07 PM
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.
tfoote #2
Posted 20 June 2012 - 11: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
Luanub #3
Posted 20 June 2012 - 11: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.


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)
Backplague #4
Posted 20 June 2012 - 11:37 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.


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.
MysticT #5
Posted 20 June 2012 - 11: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.
Backplague #6
Posted 20 June 2012 - 11:58 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.
Thanks, this works :P/>/>