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

Newb with tables help.

Started by sirdabalot, 29 September 2012 - 02:56 PM
sirdabalot #1
Posted 29 September 2012 - 04:56 PM
Spoiler

term.clear()
term.setCursorPos(1,1)
curmenu = "main"
menus = {
main = {
  options = {
  "Numbers",
  "Letters",
  "Quit",
  },
drawfunc = drawMain
},
numbers = {
  options = {
  "1",
  "2",
  "3",
   },
},
letters = {
  options = {
  "A",
  "B",
  "C",
   },
}
}
-- Functions
function drawMain()
for i = 1, 3 do
  term.setCursorPos(3,4 + i*2)
  write(menus[main].options[i])
end
end
-- Main Program
while true do
menus[curmenu].drawfunc()
end

Right so, I'm trying to make a simple GUI and am bad at using tables, my error is on line 43 (menus[curmenu].drawfunc()) and it says attempt call nill… I realise I'm probably choosing the wrong key or something but the problem is I don't know the syntax for calling stuff from tables no matter how many tutorials I read. So what's the syntax in this particular case?
Kolpa #2
Posted 29 September 2012 - 05:53 PM
Spoiler

term.clear()
term.setCursorPos(1,1)
curmenu = "main"
menus = {
main = {
  options = {
  "Numbers",
  "Letters",
  "Quit",
  },
drawfunc = drawMain
},
numbers = {
  options = {
  "1",
  "2",
  "3",
   },
},
letters = {
  options = {
  "A",
  "B",
  "C",
   },
}
}
-- Functions
function drawMain()
for i = 1, 3 do
  term.setCursorPos(3,4 + i*2)
  write(menus[main].options[i])
end
end
-- Main Program
while true do
menus[curmenu].drawfunc()
end

Right so, I'm trying to make a simple GUI and am bad at using tables, my error is on line 43 (menus[curmenu].drawfunc()) and it says attempt to call nill. I realise I'm probably choosing the wrong key or something but the problem is I don't know the syntax for calling stuff from tables no matter how many tutorials I read. So whats the syntax in this particular case?

i can only state the obvious because i never used stacking tables but if you want to call an string you have to put the "" aroud it like this write(menus["main"].options)
Kolpa #3
Posted 29 September 2012 - 05:58 PM
Spoiler

term.clear()
term.setCursorPos(1,1)
curmenu = "main"
menus = {
main = {
  options = {
  "Numbers",
  "Letters",
  "Quit",
  },
},
drawfunc = drawMain
numbers = {
  options = {
  "1",
  "2",
  "3",
   },
},
letters = {
  options = {
  "A",
  "B",
  "C",
   },
}
}
-- Functions
function drawMain()
for i = 1, 3 do
  term.setCursorPos(3,4 + i*2)
  write(menus[main].options[i])
end
end
-- Main Program
while true do
menus[curmenu].drawfunc()
end

Right so, I'm trying to make a simple GUI and am bad at using tables, my error is on line 43 (menus[curmenu].drawfunc()) and it says attempt to call nill. I realise I'm probably choosing the wrong key or something but the problem is I don't know the syntax for calling stuff from tables no matter how many tutorials I read. So whats the syntax in this particular case?

i can only state the obvious because i never used stacking tables but if you want to call an string you have to put the "" aroud it like this write(menus["main"].options)

wait nevermind this should be the real error:

term.clear()
term.setCursorPos(1,1)
curmenu = "main"
menus = {
  main = {
	options = {
	"Numbers",
	"Letters",
	"Quit",
	},
  drawfunc = drawMain
},
numbers = {
  options = {
  "1",
  "2",
  "3",
   },
},
letters = {
  options = {
  "A",
  "B",
  "C",
   },
}
}
-- Main Program
while true do
menus.drawfunc() --drawfunc is not in the main table
end
sirdabalot #4
Posted 29 September 2012 - 06:09 PM
Oh, well I derped big time. :)/>/>

EDIT: Wait, are you sure it's not in the main table? According to notepad++ it is.
Kolpa #5
Posted 29 September 2012 - 07:35 PM
in the code that i posted (which i copied) it isn't but there are more } than there are { so i guess that's an copy error

then try

menus[1].drawfunc()

and 1. of all try declaring the function before you rename it :)/>/>
sirdabalot #6
Posted 29 September 2012 - 08:31 PM
in the code that i posted (which i copied) it isn't but there are more } than there are { so i guess that's an copy error

then try

menus[1].drawfunc()

and 1. of all try declaring the function before you rename it :)/>/>

After a bit of testing I found that functions probably don't need to be placed in a different order, but anyway if I put the function above the table it gives me an error at write(menus[main].options). Furthermore if i try menus[1].drawfunc() it gives me attempt to index ? a nill value.

BTW on the opening post I said it gives me attempts to call nill error, it was actually attempt to index ? a nill value.

EDIT: I lied, by changing it to menus[1].drawfunc() it gave me attempt to index ? a nill value
MysticT #7
Posted 29 September 2012 - 09:38 PM
You need to define the function before assigning it to another variable. Also, there's an error in the drawMain function:

write(menus[main].options[i])
-- should be:
write(menus["main"].options[i])
-- or
write(menus.main.options[i])
sirdabalot #8
Posted 29 September 2012 - 09:59 PM
You need to define the function before assigning it to another variable. Also, there's an error in the drawMain function:

write(menus[main].options[i])
-- should be:
write(menus["main"].options[i])
-- or
write(menus.main.options[i])

Okay, so let me clear one last thing up. When do I use a ., when do I use [] and when do enclose in quotes?
MysticT #9
Posted 29 September 2012 - 10:51 PM
This are the same:

t["key"]
t.key
It will get the value stored on the table with the string "key" as key.
This:

t[key]
will get the value in the table with the content of the variable "key" as key. So, if the variable key contains the number 2, it would be like doing

t[2]
sirdabalot #10
Posted 29 September 2012 - 11:05 PM
This are the same:

t["key"]
t.key
It will get the value stored on the table with the string "key" as key.
This:

t[key]
will get the value in the table with the content of the variable "key" as key. So, if the variable key contains the number 2, it would be like doing

t[2]

And it works with numbers too? So if I had table t and had a value at index 3 I could use:

t.3

As well as:

t[3]
MysticT #11
Posted 29 September 2012 - 11:12 PM
No, it only works with strings.
sirdabalot #12
Posted 30 September 2012 - 09:09 AM
No, it only works with strings.

Okay, thanks for your time. :)/>/>