Reputation: 23
Hello there I'm new in Lua and a have a question, I have this code:
dialogExample = "1. han3x1 2. 1kekeo 3. 103kdo 4. 54o4as 5. asbf1c 6. ask6os"
for k in string.gmatch(dialogExample, "[^%s]+") do
print(k)
end
output:
1.
han3x1
2.
1kekeo
3.
103kdo
4.
54o4as
5.
asbf1c
6.
ask6os
How can I get the the values by the index, for exemple, I just want to print the value of the line 1. in this case "han3x1", How can I do this?
I want get just the "1. han3x1", after I just want the value "4. 54o4as". But these values are random
I appreciate any help, thank's
Upvotes: 2
Views: 90
Reputation: 2793
Just use a table
-- table with index keys from 1 to 6
dialogExample = {"han3x1", "1kekeo", "103kdo", "54o4as", "asbf1c", "ask6os"}
for i = 1, #dialogExample do
io.write(('%d.\n%s\n'):format(i, dialogExample[i]))
end
Output:
1.
han3x1
2.
1kekeo
3.
103kdo
4.
54o4as
5.
asbf1c
6.
ask6os
Than...
io.write(('%d.\n%s\n'):format(1, dialogExample[1]))
Output:
1.
han3x1
Upvotes: 1
Reputation: 2205
perhaps your case describes a pattern like this:
local dialogExample = "1. han3x1 2. 1kekeo 3. 103kdo 4. 54o4as 5. asbf1c 6. ask6os"
local t = {}
for i,v in string.gmatch(dialogExample, "(%d+)[%.%s]*([^%s]+)") do
t[i] = v
print(i,t[i])
end
out:
1 han3x1
2 1kekeo
3 103kdo
4 54o4as
5 asbf1c
6 ask6os
Upvotes: 3