Jump to content




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


  • You cannot reply to this topic
5 replies to this topic

#1 Backplague

  • New Members
  • 38 posts
  • LocationEarth

Posted 20 June 2012 - 10: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.

#2 tfoote

  • New Members
  • 134 posts
  • LocationSalt Lake, UT

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 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

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.

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 Backplague

  • New Members
  • 38 posts
  • LocationEarth

Posted 20 June 2012 - 10:37 PM

View Postluanub, 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.

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 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

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 Backplague

  • New Members
  • 38 posts
  • LocationEarth

Posted 20 June 2012 - 10:58 PM

View PostMysticT, 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.
Thanks, this works :P/>





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users