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

attempt to call index ? - but the table is there!

Started by excubated, 30 March 2014 - 10:18 AM
excubated #1
Posted 30 March 2014 - 12:18 PM
Hi!

I just got into CC and Lua coding, and I'm currently working on a fully scalable IC2 energy monitoring program. My code runs fine until line 148, where I get the "attempt to call index ?" error. I have set the table with pbat = {}, and I'm trying to add to the table with a for loop.

  for i = 1,batboxes do
	[b]pbat[i] = batbox[tostring(i)].getEUStored()[/b] --Line 148
	cbat[i] = batbox[tostring(i)].getEUCapacity()
	print("BatBox", i, ": ", pbat[i], " EU / ", cbat[i], " EU")
  end
I wrote a simpler program with the same piece of code in it earlier, and that ran fine..

Current program, returns error on line 148:
http://pastebin.com/JrfmPEm2

Last program, runs fine (line 20 here is the same variable/string/code thingy I'm struggling with on the current program):
http://pastebin.com/YMKhrJ8n

I am aware this error falls under the "common errors" category, but I can't seem to understand why I didn't get the error in my last program.
CometWolf #2
Posted 30 March 2014 - 06:06 PM

    batbox[i] = peripheral.wrap("batbox_[tostring(i - 1)]")
This won't work, i doubt you have a peripheral called "batbox_[tostring(i -1)]" lol

    batbox[i] = peripheral.wrap("batbox_"..tostring(i - 1)
This is however a silly way of doing it, you should just wrap them right off the bat

local tBatbox = {}
for _,name in pairs(peripheral.getNames()) do
  if name:match"batbox" then
    tBatbox[#tBatbox+1] = peripheral.wrap(name)
  end
end
excubated #3
Posted 31 March 2014 - 12:50 AM
Thanks CometWolf, it really makes sense to wrap them when I'm scanning for them in the first place.. Two quick followup questions though:

1: In your second code tag,, you've written "batbox_"..tostring(i - 1))
What does the two periods do in the code?

2: In your second code tag, there's a hash sign in the brackets.
My guess would be that this tells the computer to interpret the table info as a number rather than a string, is this correct?

Last, I've encountered a possible obstacle while dicking arould in lua>. When i do ..

mfe = peripheral.wrap("mfe_1")
p = mfe.getEUStored()
print(p)
..i get the MFE's currently stored power in one line, and the number "1" on the next one. I don't know if this only happens in "raw" Lua input, or if the same thing might bug up my program?
Inumel #4
Posted 31 March 2014 - 02:40 AM
the two dots are concatenation , basically combining anything they are between.
and the # does is used to get the length of something, when used on a table it gives how many things are in said table
and i believe the "1" is just saying its true, and yes only in the lua prompt, i believe it would give a "0" if it fails to do the task
wieselkatze #5
Posted 02 April 2014 - 08:10 AM
Actually you wont even need that tostring(i-1), because it automatically gets formatted to a string.
So this would just be

peripheral.wrap("batbox_"..(i-1))

Aside from that CometWolfs code won't work, because he is missing a bracket at the end.
CometWolf #6
Posted 02 April 2014 - 11:14 AM
Yeah, didn't really put much into that one.
This is however a silly way of doing it, you should just wrap them right off the bat