lua-users home
lua-l archive

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


On 20 January 2014 12:32, Hendrik Werner <hendrik.to@gmail.com> wrote:
> 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?

The table is a value. Any names you bind to it are references,
including function parameters.

(By the way, something is considered anonymous when it can't be
accessed by name. If you create a value and bind it to the name "a",
well you've just given it a name and thus it's not "anonymous" at
all.)

> 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

Yes, but are you sure that's what you want to do?