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

Variable as Key when pulling from table driving me insane!

Started by fugigas, 09 December 2013 - 10:27 PM
fugigas #1
Posted 09 December 2013 - 11:27 PM
Hi All

PasteBin - Code
PasteBin - TxtFile

When running the attached code I am getting "opencc:22: attempt to index ? (a nil value)" error thrown up.

So i replaced the "foo" variable with just the written out string "4,-1,-2" and it calls the table and runs the program perfectly. I cannot, for the life of me, understand the difference between the "foo" variable being written plain or being written by the "tabID" argument is?!

So just to clarify:

This works

function getCount(tabID)
  print(tabID)
  foo = "4,-1,-2"
  tabInv = inv.getTargets()[foo]
  invCount = tabInv["ItemCount"]
  return invCount
end

This does not

function getCount(tabID)
  print(tabID)
  foo = tostring(tabID)
  tabInv = inv.getTargets()[foo]
  invCount = tabInv["ItemCount"]
  return invCount
end

But printing this gives exactly the same output (visibly in-game anyway)

function getCount(tabID)
  foo = "4,-1,-2"
  print(tostring(tabID))
  print(foo)
end

Any help would be much appreciated! :D/>
MKlegoman357 #2
Posted 10 December 2013 - 08:37 AM
Your problem is that in your TxtFile you surround values ("4,-1,-2") by quotes but when getting the value you get it with quotes ("(%w+)%s=%s(%S+)"). What you could do is get value inside quotes by adding them in the pattern:


"(%w+)%s=%s\"(%S+)\""

--// You need to write \" so the interpreter wouldn't think you want to close the string (pattern)

That pattern will then get value from inside of those quotes.

Or you could just remove all quotes from that file.
fugigas #3
Posted 10 December 2013 - 09:23 AM
Thanks you so much :)/>

Made some modifications to the string matching pattern and we are back in the game!

I think i was getting hung up on the idea that there was a hidden /n character on the end of the string that I couldn't see.