lua-users home
lua-l archive

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


* On Sat Mar 30 17:17:24 +0100 2013, Mason Mackaman wrote:
 
> function f(set)
>     local newset,cloneset={},set
>     for x=1,#cloneset do
>         newset[x]=cloneset[#cloneset]
>         table.remove(cloneset)
>     end
>     return newset
> end
> 
> function g(set)
>     set=f(set)
> end
> 
> a={1,2,3,4,5,6,7,8,9}
> g(a)
> print(#a)

You pass 'a' trough 'g()' into 'f()'. f() modifies this table by
removing all it's entries, effectively modifying 'a'. g() modifies the
'set' variable, but this does not mean it also changes 'a' in the global
scope, it merely changes the local varable 'set', which happens to point
to the same table as 'a'.
-- 
:wq
^X^Cy^K^X^C^C^C^C