Jump to content




help with tables/variables needed


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

#1 tom2018

  • Members
  • 135 posts

Posted 27 October 2012 - 04: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

#2 Jan

  • Members
  • 141 posts
  • Locationthe Netherlands

Posted 27 October 2012 - 04: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

#3 ChunLing

  • Members
  • 2,027 posts

Posted 27 October 2012 - 05: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.

#4 tom2018

  • Members
  • 135 posts

Posted 27 October 2012 - 05:46 PM

thanks

#5 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 27 October 2012 - 07:30 PM

Yeah, string.gmatch is useful:

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






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users