lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I could not think of better title, sorry for that so here's my problem.
I have a function like this:

function foo(a)
    -- do something with a
    return a
end

and my call to the function looks as follows:

a = {}
-- fill table with something
a = foo(a)

and that works the way I want it to work. But now I wanted to alter the function so it checks whether or not the operation is possible on a but does not actually execute it so I tried the following:
I tried to edit the function call like this:

a = {}
-- fill table with something
foo(a)

but a still changes!! I also tried to remove the return part:

function foo(a)
    -- do something with a
end
a = {}
-- fill table with something
foo(a)

but a STILL changes... even when I do this

function foo(a)
    -- do something with a
    return a
end
a = {}
-- fill table with something
b = a
foo(b)

that still changes the value of a... I figured it may have something to do with the fact that "b = a" does not actually create another variable but another reference to the anonymous value a references (that's the way it works, right?) so the implicit definition "local a = a" in my function header also just creates another reference to the same anonymous value thus also changes that value, is that right so far? Do I really need something like:

for i = 1, #a do
    b[i] = a[i]
end

every time I want to call a function on a that does not change it? I hope you understand my problem and can help me, thanks
hendrikto