lua-users home
lua-l archive

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


By "to make a Lua variable reference another variable", I guess he meant
something like this in C:

Program:
int *ap, a = 11;
ap = &a;
a = 12;
printf("%d\n", *ap);

Output:
12

This is not possible in Lua with primitive types to my knowledge. You cannot
setup multiple variables with different names pointing to the same primitive
in memory. So you'll either need to use a table for example, to wrap your
variable, so be able to point to the same variable with multiple names, or
store the variable name in a string and use that to index the table it is
in.

Program:
a = { v = 11 }
ap = a
a.v = 12
io.write(tostring(ap.v).."\n")

a = 11
ap = "a"
a = 13
io.write(tostring(_G[ap]).."\n")

Output:
12
13

I consider myself no Lua expert however, so maybe there are better ways.

Bjørn


----- Original Message -----
From: <RLake@oxfam.org.pe>
To: "'Lua list'" <lua@bazar2.conectiva.com.br>
Sent: Wednesday, May 28, 2003 10:23 PM
Subject: Starting Lua


> > is there any way to make a Lua variable reference another variable?
>
> A Lua global is actually a key in the global environment table. So if _G
is
> the global environment table (which it is if you haven't changed it with
> setfenv and you are using the standalone Lua interpreter) then
>
>   a
>
> is the same as
>
>   _G.a
>
> is the same as
>
>   _G["a"]
>
>
> Hope that helps