This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Dave-ee Jones's profile picture

[SOLVED] Can't get length of first index in a table?

Started by Dave-ee Jones, 25 July 2017 - 02:00 AM
Dave-ee Jones #1
Posted 25 July 2017 - 04:00 AM
Hey,

So my code looks something like this:


local t = {
  "string",
  "string2",
  "string3"
}

local k = 1

while true do
  for i=1,#t[k] do
	print(string.sub(t[k],i,i))
  end
  sleep(0)
end

But it keeps telling me this error:
"test:10: attempt to get length of nil"

What I'm trying to do is get the length of the strings in the table, printing out each character 1 by 1, but instead of doing that it errors on the first one, but then prints it out and then the rest? It's so weird, because 'k' is equal to 1 which is the first index of the table but it's like it's saying that I'm trying to get the length of the string of index 0, but it's clearly 1.

Why is this happening?
Edited on 25 July 2017 - 04:26 AM
Dog #2
Posted 25 July 2017 - 04:56 AM
I ran the script as posted and it works as you said it should - no problems.

It would help to see the actual code you're working with…
Dave-ee Jones #3
Posted 25 July 2017 - 05:04 AM
Ah, never mind! I was using the same variable name elsewhere (being 'k' I was using it a lot for 'for' statements..), and it got confused. Thanks Dog, was a silly mistake :/