Jump to content




Inserting variables into a table


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

#1 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

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


#2 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 04 December 2012 - 01:56 PM

View PostCranium, on 04 December 2012 - 11:45 AM, said:

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.

#3 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 04 December 2012 - 01:58 PM

Hmm....
Can you complicate it more? I really want to make it SUUUPER hard to use. ;)

#4 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 04 December 2012 - 02:06 PM

View PostCranium, on 04 December 2012 - 01:58 PM, said:

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 :)/>

#5 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

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

#6 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

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 by KaoS, 05 December 2012 - 12:32 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users