Jump to content




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


  • You cannot reply to this topic
5 replies to this topic

#1 discodancepant

  • Members
  • 12 posts

Posted 31 May 2013 - 05: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

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

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 by Lyqyd, 31 May 2013 - 10:25 PM.


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 31 May 2013 - 10: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().

#3 discodancepant

  • Members
  • 12 posts

Posted 01 June 2013 - 05: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?

#4 discodancepant

  • Members
  • 12 posts

Posted 01 June 2013 - 06: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.

#5 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 01 June 2013 - 06: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.

#6 discodancepant

  • Members
  • 12 posts

Posted 01 June 2013 - 07: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





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users