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

Inserting variables into a table

Started by Cranium, 04 December 2012 - 10:45 AM
Cranium #1
Posted 04 December 2012 - 11:45 AM
Before you ask, YES I know how to put variables into a table. What I am asking is how to I put variables into an index OF a table? For example:

local digits = {
[1] = {
".---.",
"| 1 |",
"'---'"}
}
Where I would like to be able to ONLY make that ONE index. And be able to replace the index number "1" and the string number "1" so I only have to call to it once. Is there a way to do that?

I have a feeling that I have to do something like this:

local function digits(n)
local button = {
[n] = {
  ".---.",
  "| "..n.." |",
  "'---'"}
}
return button[n]
end
Bubba #2
Posted 04 December 2012 - 01:56 PM
Before you ask, YES I know how to put variables into a table. What I am asking is how to I put variables into an index OF a table? For example:

local digits = {
[1] = {
".---.",
"| 1 |",
"'---'"}
}
Where I would like to be able to ONLY make that ONE index. And be able to replace the index number "1" and the string number "1" so I only have to call to it once. Is there a way to do that?

I have a feeling that I have to do something like this:

local function digits(n)
local button = {
[n] = {
  ".---.",
  "| "..n.." |",
  "'---'"}
}
return button[n]
end

I don't see the issue - your funtion works doesn't it? I don't see any reason it should not. There is a way to make your table behave as a function and accept arguments using a metatable, but that really overcomplicates things and in my opinion isn't worth it in this case.
Cranium #3
Posted 04 December 2012 - 01:58 PM
Hmm….
Can you complicate it more? I really want to make it SUUUPER hard to use. ;)/>
Bubba #4
Posted 04 December 2012 - 02:06 PM
Hmm….
Can you complicate it more? I really want to make it SUUUPER hard to use. ;)/>/>/>

Lol. Actually this probably makes it easier to use, just harder to code. Just in case you want to know how to do it, here is an example:


local digits = { --Set this to the default value of the button
[1] = {
".---.",
"| 1 |",
"'---'"}
}

local mt = { __call = function(table, n) --This function will replace the number in the button
digits = {
[n] = {
  ".---.",
  "| "..n.." |",
  "'---'"}
}
end
}
setmetatable(digits, mt)

--And to change the value of digits[n], just do this
digits([The number you want to set it to])


Hope this helps :)/>/>
KaoS #5
Posted 04 December 2012 - 08:01 PM
you use metatables as Bubba said but I think there may be a simpler way


local digits=setmetatable({},{__index=function(t,index) return {".---.",
"| "..index.." |",
"'---'"} end})

that metatable would call the function there whenever you tried to access a table index that has no value, the function will return a fake value so you never actually need to define a button. this will not work with pairs loops and may generate infinitely with ipairs loops so don't use it with loops k :)/> it can be modified to work like Bubba's idea too when you index a value so you can actually prepare and use a for loop
KaoS #6
Posted 05 December 2012 - 12:10 AM
ok here are another 2 versions you may find useful


local digits=setmetatable({},{__index=function(t,index) t[index]={".---.",
"| "..index.." |",
"'---'"} return t[index] end})
will add the value to the table in addition to faking it when requested


local digits
do
  local tNums={}
  digits=setmetatable({},{__index=function(t,index) if tNums[index] then return {".---.",
"| "..index.." |",
"'---'"} end end,__newindex=function(t,index) tNums[index]='.' end,__call=function(t) return pairs(tNums) end})
end
would make a hidden table caled tNums where it stores '.' on certain numbers and those numbers will give you a value, if you want to parse it with pairs do the following

for k,v in digits() do
  print(digits[k])
end

DO NOT USE THE 'v' AS IT IS ALWAYS '.'

EDIT: there is an infinitely simpler method as follows


local digits=setmetatable({},{__newindex=function(t,index) rawset(t,index,{".---.",
"| "..index.." |",
"'---'"}) end})

if you try to add any value to the digits table it will automatically modify it to be in the format you showed in the OP
Edited on 04 December 2012 - 11:32 PM