[Resolved] String to array

Is there a way to turn a single word into an array? For example if I have the following:

local myString = "range"  
local myTable = {}  

and I want to split the word range into the following:

myTable = {"r","a","n","g","e"}  

is there a way to do this in Lua? I would like to do this with string manipulations instead of hard coding it because myString will most likely hold random letters.

Thank you for your help!!! [import]uid: 14218 topic_id: 27328 reply_id: 327328[/import]

Hi Daniel,

I believe this will help you;

[lua]local myString = “range”
local myTable = {}

for i = 1, string.len(myString) do
myTable[i] = myString:sub(i,i)
print (myTable[i])
end[/lua]

Or if you wanted to reuse it multiple times something like this should do the trick;
[lua]local myString = “range”
local myTable = {}

local function doTable(str)
for i = 1, string.len(str) do
myTable[i] = str:sub(i,i)
print (myTable[i])
end
end
doTable(myString)[/lua]

The only thing that doesn’t do, if using multiple times, is clearing out myTable[6] if the previously work was 6 chars and the current is only 5, so take that into account too if need be.

Let me know if that helps :slight_smile:

Peach [import]uid: 52491 topic_id: 27328 reply_id: 111039[/import]

Thank you Peach!!! By the way, I’m not sure how new the avatar photo is, but I love it. Thank you again. [import]uid: 14218 topic_id: 27328 reply_id: 111066[/import]

No worries Daniel, I’ve never written code for this purpose before so it was actually a lot of fun for me.

The avatar is from a week or two ago - thank you for the kind words, most appreciated!

Peach :slight_smile: [import]uid: 52491 topic_id: 27328 reply_id: 111111[/import]