Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I use a table in string.find?

Asked by
iAviate 20
8 years ago

Hi,

table[1] is the first index of a table. Its value is the string 'Color: Orange'

Here i'm trying to just get the word 2 characters after the ':', using string.find then string.sub

firstkey = table[1]

pos = string.find(""..firstkey, ":") -- Position of ':'

after = ( string.sub(""..firstkey, pos+2) ) -- Everything from two characters after ':' onwards

print(after)

It doesn't work though, and the output says that it expected a string for string.find, instead it got a table.

Why is this and how can I fix it?

Thanks in advance.

0
Can you show us what the variable table is? That might help determine the problem. dyler3 1510 — 8y
0
It's not the problem - print(table1) prints Color: Orange iAviate 20 — 8y
0
I don't get it. Works perfectly for me if I replace table[1] with "Color: Orange". What are the contents of table? XAXA 1569 — 8y
0
It's just [1]=Color: Orange, [2]=Color: Blue, [3]=Color: Green etc.. iAviate 20 — 8y
View all comments (6 more)
0
fyi, those should be surrounded by by quotes or [[]] (e.g. table = {[1] = "Color: Orange", [1] = "Color: Green"} etc.) Consider storing the name of the color itself without the "Color: " part. XAXA 1569 — 8y
0
For the sake of writing the table in the comments, I simplified it, and I am also unable to change what it's stored as because i'm working with someone else's table. iAviate 20 — 8y
0
Mind pasting the full table in the comments? No simplification, please. XAXA 1569 — 8y
0
I don't exactly HAVE the table - i'm reading it. iAviate 20 — 8y
0
I ran this code: http://hastebin.com/ayalejayat.lua It works as expected. There's something wrong with your table. Ask whoever wrote the table how they formatted it. You may want to do something like `for i, v in pairs(table) do print( i, v ) end` to check how the table is formatted yourself. XAXA 1569 — 8y
0
Will do - thanks! iAviate 20 — 8y

Answer this question