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

How to separate strings into a table?

Started by Agent Silence, 29 May 2014 - 11:29 PM
Agent Silence #1
Posted 30 May 2014 - 01:29 AM
Imagine I wanted to split lets say "Hello World!" into a table and the table show up as :
example = {"H","e","l","l","o"," ","W","o","r","l","d","!"}
Does anybody know how to do this? All help is appreciated
Bomb Bloke #2
Posted 30 May 2014 - 01:38 AM
Well, you could put all the characters into a table by iterating through the string with string.sub, I suppose, but I suspect you'd be better off leaving them where they are and string.sub'ing them out when you actually need them.

In any case, assuming you're already familiar with tables and loops and such, have a read through the string-related functions here.
apemanzilla #3
Posted 30 May 2014 - 05:23 AM
Well, normally I wouldn't write code for AAP, but it's pretty simple, so meh:

function toTable(str)
  local tbl = {}
  for i = 1, #str do
    table.insert(tbl,string.sub(str,i,i))
  end
  return tbl
end
Edited on 30 May 2014 - 03:24 AM