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

table inside table error: "attempt to index ? (a number value)

Started by discodancepant, 31 May 2013 - 04:42 PM
discodancepant #1
Posted 31 May 2013 - 06:42 PM
I have looked for over an hour for an answer to this, so if the answer is oviosu, i tried finding it first, just FYI.

I have a table within a table which looks like this:

Spoiler

local desktopGrid = {
[1] = {
  GridBeginX = 2,
  GridBeginY = 2,
  GridEndX = 10,
  GridEndY = 6,
  desktopGoTo = 1,
  [1] = {
   GridBeginX = 2,
   GridBeginY = 2,
   GridEndX = 10,
   GridEndY = 2,
   onClick = 1
  },
  [2] = {
   GridBeginX = 2,
   GridBeginY = 3,
   GridEndX = 10,
   GridEndY = 3,
   onClick = 2
  }
},
[2] = {
  GridBeginX = 13,
  GridBeginY = 1,
  GridEndX = 50,
  GridEndY = 18,
  desktopGoTo = 2
}

}

Inside my first table i have regular indexes and values which define
desktopGrid[i]
then after those first values i have indexes which create more tables.

I call this table into action using:

Spoiler

for a,v in pairs(desktopGrid) do
	if (arg2 >= (desktopGrid[a].GridBeginX)) and (arg2 <= (desktopGrid[a].GridEndX)) then
		if (arg3 >= desktopGrid[a].GridBeginY) and (arg3 <= desktopGrid[a].GridEndY) then
			if desktopGrid[a].desktopGoTo == 1 then
				for b,v in pairs(desktopGrid[a]) do
					if (arg2 >= desktopGrid[a][b].GridBeginX) and (arg2 <= desktopGrid[a][b].GridEndX) then
						if (arg3 >= desktopGrid[a][b].GridBeginY) and (arg3 <= desktopGrid[a][b].GridEndY) then
							if (menuItemSelect == 5) and (desktopGrid[a][b].onClick == 5) then
								escape = true
							end
							menuItemSelect = desktopGrid[a][b].onClick
						end
					end
				end
			elseif desktopGrid[a].desktopGoTo == 2 then
				
			elseif desktopGrid[a].desktopGoTo == 3 then
				menuItemSelect = 0
			end
		end
	end
end

which returns my afore-mentioned error. my question is; do i get this fault because my table has strings and numbers in it and therefore this will never work, or is there a way around this fault?

I am able to get the code to work when i split the sub table into its' own table, but i would rather have one table than multiple, if this is possible.
Edited on 31 May 2013 - 09:25 PM
Lyqyd #2
Posted 31 May 2013 - 11:25 PM
Split into new topic.

pairs() serves up every index in the table, so the inner one is giving you both your numeric indicies (which you want) and your string indices (which you don't want, as they don't have tables as their values). Try switching the inner pairs() out for an ipairs().
discodancepant #3
Posted 01 June 2013 - 06:08 PM
about 30 mins after posting this, i had been messing with ipairs, and it wasn't working.
then i switched the indexes so that numeric came first. everthing worked fine after that. now i have another table - related issue:

i have a simple table, with a few "starting" values; basically filler text.

farming = {
	[1] = {
		XCoord = "-----",
		YCoord = "-----"
	},
	[2] = {
		XCoord = "-----",
		YCoord = "-----"
	},
}

i have another table which references them among other things:
local desktopGrid = {
	[2] = {
		[1] = {
			[1] = {
				[3] = {line = 2,indent = 0,button = false,textField = true,buttonBegin = 15,buttonEnd = 19,text = "North East X: [	 ]",onClick = farming[1].XCoord},
				[4] = {line = 3,indent = 0,button = false,textField = true,buttonBegin = 15,buttonEnd = 19,text = "North East Y: [	 ]",onClick = farming[1].YCoord},
				[5] = {line = 4,indent = 0,button = false,textField = true,buttonBegin = 15,buttonEnd = 19,text = "South West X: [	 ]",onClick = farming[2].XCoord},
				[6] = {line = 5,indent = 0,button = false,textField = true,buttonBegin = 15,buttonEnd = 19,text = "South West Y: [	 ]",onClick = farming[2].YCoord},
			},
		}
	}
}

then i use the following code to change my original table:

desktopGrid[2][1][menuItemSelect][e].onClick = read()

this works just fine… while my program is running. the table changes and i am able to write those changes to the screen.
However; when i attempt to write the farming table to a file, it only outputs the filler values that i started with.

i found that when i point read() directly at the first table, it writes like it should. but when i point it indirectly, it doesn't. I have an idea of a workaround, i just wonder if anyone can explain why this happens?
discodancepant #4
Posted 01 June 2013 - 07:20 PM
shoot i got myself… again. when i'm calling the onClick function, instead of getting directed to the farming table, i'm changing the other table's value instead, which made it appear like i was changing my farming table.


it's funny how i spend hours trying to figure something out, then when i leave for a few minutes, I'll randomly have the answer.
Bomb Bloke #5
Posted 01 June 2013 - 07:44 PM
Note that if you're only using numeric indexes, you don't have to specify them all, you just dump them in. Only works sequentially though, meaning you have to specify nil for those indexes you want to skip.

local desktopGrid = 
        {nil,
          {
            {
              {nil,
               nil,
	       {line = 2,indent = 0,button = false,textField = true,buttonBegin = 15,buttonEnd = 19,text = "North East X: [       ]",onClick = farming[1].XCoord},
               {line = 3,indent = 0,button = false,textField = true,buttonBegin = 15,buttonEnd = 19,text = "North East Y: [       ]",onClick = farming[1].YCoord},
               {line = 4,indent = 0,button = false,textField = true,buttonBegin = 15,buttonEnd = 19,text = "South West X: [       ]",onClick = farming[2].XCoord},
               {line = 5,indent = 0,button = false,textField = true,buttonBegin = 15,buttonEnd = 19,text = "South West Y: [       ]",onClick = farming[2].YCoord}
               }
             }
           }
	 }

You may find your way easier to read, which is fine. This is simply another way of getting the same result.
discodancepant #6
Posted 01 June 2013 - 08:16 PM
oh, yeah, actually that's useful information i seem to have looked over.
in this instance i need the numeric index, but i will certainly remember that for future table endeavors! thanks