FICHEKK
FICHEKK

Reputation: 819

Changing variable in a table

How can I make a changing variable an element of a table, like so.

local table = {}
local var = 10

Now I want to insert this variable as an element of table. Something like this:

table[1] = var

What I need is, whenever I call this table[1], even if the variable changes, it will call the actual value of that variable, like this:

print(table[1]) -> prints 10
var = var + 5
print(table[1]) -> prints 15

Is this even possible somehow?

EDIT: What I want to accomplish is the following: I want to have an element in table that says which variable should be shown. For example:

local var1 = 10
local var2 = 20

Now if I have a table that has elements as strings of those variables like so:

local table = {"var1", "var2"}

Now if I do print(table[1]), of course it will print out "var1", but is there any way I could turn this element of a table that is string into a call for variable when I actually need that variable. You might be asking why don't I just call the var1, but there is a reason which I can explain, but it would be really long. Let's say I just need it this way. Also, the var1/var2 CAN CHANGE.

Upvotes: 2

Views: 865

Answers (2)

Tom Blodget
Tom Blodget

Reputation: 20802

You have a couple of choices.

1) Field function as a closure over var: Straightforward but requires changes how you use it

local t = {}
local var = 10
t.varf = function() return var end -- varf could be named var but that might be confusing

var = var + 5
print(t.varf()) -- call it to get the value

2) __index metamethod to avoid the explicit function call syntax

local t = {}
local var = 10
setmetatable(t, { 
    __index = function(_, k) 
        if k=="var" then return var else return nil 
    end})

var = var + 5
print(t.var) -- t does not contain a field with key "var" so __index is called 

(The __index function is also a closure over var.) If you want to modify var via t, then look to the __newindex metamethod.

Both methods use closures. A closure is a function that refers to non-global variables outside of its parameters and body.

Upvotes: 4

Oka
Oka

Reputation: 26355

Numbers in Lua are shared by copy, at time of assignment. The table[1] and var both receive their own, independent copy of the number 10.

If you want to share numbers, you'll need to encapsulate them in their own table.

local table = {}
local var = { value = 10 }

table[1] = var

print(table[1].value) -- prints 10
var.value = var.value + 5
print(table[1].value) -- prints 15

You can also consider creating some kind of abstraction over numbers. A quick example. You'll need to make sure your operations are well defined, though.

local number = {}
number.__index = number

local function Number (value)
    return setmetatable({ value = value }, number)
end

function number.__add (a, b)
    if type(b) == 'number' then
        return Number(a.value + b)
    elseif getmetatable(b) == number then
        return Number(a.value + b.value)
    end

    error("one of `number, Number' expected")
end

function number:add (n)
    if type(n) == 'number' then
        self.value = self.value + n
    elseif getmetatable(n) == number then
        self.value = self.value + n.value
    else
        error("one of `number, Number' expected")
    end

    return self.value
end

function number.__tostring (v)
    return v.value .. ''
end



local foo = {}
local bar = Number(10)
foo[1] = bar

print(foo[1]) -- 10
bar:add(5)
print(foo[1]) -- 15
print(bar + 25) -- 40

Upvotes: 2

Related Questions