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

Possible to "separate" a string?

Started by Jtpetch, 31 August 2016 - 02:15 AM
Jtpetch #1
Posted 31 August 2016 - 04:15 AM
So, say that the variable x=read(), and the user enters "Hello"
Is there any way to separate this into 5 strings"H" "e" "l" "l" "o" which can be used later in the program?
The only way I could think of would be using a function instead of a variable at the beginning, but then I'm not sure how the read() would initially be entered into it.

A bit of research found a "Split/Join" function in Lua, but I'm not sure whether or not it could do this.
Dog #2
Posted 31 August 2016 - 04:34 AM
The easiest way I can think of off the top of my head would be using a table and string.sub(). Try this…

local strings = { } --# declare and localize your strings table
local x = read()  --# read user input using a local variable
for i = 1, #x do  --# start a loop that loops for as many times as there are characters in the user input ('i' will start counting at 1 and stop at the number of characters in 'x')
  strings[i] = x:sub(i, i) --# use string.sub() to put each character in its own entry in the strings table (i = the number of the current character we're handling)
end --# end the loop

If the user inputs "Hello" then your strings table would be as follows…

strings[1] = "H"
strings[2] = "e"
strings[3] = "l"
strings[4] = "l"
strings[5] = "o"

If the user inputs "Hi" then the strings table would be…

strings[1] = "H"
strings[2] = "i"

If you want to capture user input more than one time, then each time you want to capture user input you need to clear the strings table of the previous input, like so…

for i = #strings, 1, -1 do --# start a loop that goes backwards through the strings table, from the end to the beginning
  strings[i] = nil --# set each strings entry to nil
end --# end the loop

So, in the end, your code would look something like this…

local x --# declare and localize your input variable
local strings = { } --# declare and localize your strings table

local function getUserInput() --# declare a localize our user input function
  if strings[1] then --# if there is data in the table, we need to clear it
    for i = #strings, 1, -1 do
      strings[i] = nil
    end
  end
  x = read()
  for i = 1, #x do
    strings[i] = x:sub(i, i)
  end
end

--# your code here
--# any time you want to get user input, simply call getUserInput()

Hope that helps :)/>
Edited on 31 August 2016 - 02:47 AM
Bomb Bloke #3
Posted 31 August 2016 - 04:52 AM
Though, depending on your intended usage, it may simply be easier to simply call string.sub() at the time you need the individual characters, rather than putting them into a table right away. It depends on how often you want to use them - basically, if the answer is "more than once", you're better off building the table.
Anavrins #4
Posted 31 August 2016 - 04:56 AM
Alternate method to using sub is using gmatch

local str = {}
for letter in x:gmatch(".") do #-- "." matches all character
  str[#str+1] = letter
end