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.

10 comments

0

Can you show us what the variable table is? That might help determine the problem.

0

It's not the problem - print(table1) prints Color: Orange

0

I don't get it. Works perfectly for me if I replace table[1] with "Color: Orange". What are the contents of table?

0

It's just [1]=Color: Orange, [2]=Color: Blue, [3]=Color: Green etc..

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.

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.

0

Mind pasting the full table in the comments? No simplification, please.

0

I don't exactly HAVE the table - i'm reading it.

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.

0

Will do - thanks!