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

help with tables/variables needed

Started by tom2018, 27 October 2012 - 03:03 PM
tom2018 #1
Posted 27 October 2012 - 05:03 PM
Ok i would like to know how i would take
x = "hello"
and turn it into
x = {"h","e","l","l","o"}
thanks
Jan #2
Posted 27 October 2012 - 05:26 PM

x = "hello"
y= {}
for a=1,#x do
y[a]=x:sub(a,a)
end
using #sometext you can get the length of a string
using "derp":sub(a,b ) you can get the string between position a and b
ChunLing #3
Posted 27 October 2012 - 06:36 PM
string.gmatch is a good way to turn strings into table elements, because it's iterative so it works well with a for v in type loop. But you can do the same with an iterative loop that uses string.sub and the #x.

Of course, since you can already access any individual character in a string using string.sub, you can probably just use the string as it is for most things.
tom2018 #4
Posted 27 October 2012 - 06:46 PM
thanks
Lyqyd #5
Posted 27 October 2012 - 08:30 PM
Yeah, string.gmatch is useful:


str = "hello"
strTable = {}
for char in string.gmatch(str, ".") do
  table.insert(strTable, char)
end