lua-users home
lua-l archive

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


Hi,

I've been very busy lately, so that is why I'm ãnswering this late.

I've got the code, but how can I use it now? I tried to print the variable access, but without success. Can you please provide me some examples?

Darius

----- Original Message ----- From: "Gé Weijers" <ge@weijers.org>
To: "Lua list" <lua@bazar2.conectiva.com.br>
Sent: Sunday, February 04, 2007 4:50 PM
Subject: Re: manipulating data


Darius,

You could add something like this small module (code at the end) to
your application, which would provide the glue between Lua and your
data structure.

In your application you'd do something like

lua_pushcfunction(L, luaopen_access);
status = lua_pcall(L, 0, 0, 0);

right after the application finishes initializing the Lua libraries.
This would add the 'access.getter' and 'access.setter' primitives to
Lua.

Hope this helps.

Groetjes,

Gé

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

static int getter(lua_State *L)
{
     lua_Integer a = luaL_checkinteger(L, 1);
     lua_Integer b = luaL_checkinteger(L, 2);
     luaL_argcheck(L, a > 0, 1, "should be positive");
     luaL_argcheck(L, b > 0, 2, "should be positive");

     /* retrieve the value */

     lua_pushnumber(L, 1.234);
     return 1;
}

static int setter(lua_State *L)
{
     lua_Integer a = luaL_checkinteger(L, 1);
     lua_Integer b = luaL_checkinteger(L, 2);
     lua_Number v = luaL_checknumber(L,3);
     luaL_argcheck(L, a > 0, 1, "should be positive");
     luaL_argcheck(L, b > 0, 2, "should be positive");

     /* store the value */
     (void)v;

     return 0;
}

/*
* The primitive tables
*/
static const luaL_Reg accesslib[] = {
     { "getter",  getter     },
     { "setter",   setter      },
     { NULL,     NULL    }
};

/*
* Install the primitives
*/
int luaopen_access(lua_State *L)
{
     luaL_register(L, "access", accesslib);
     return 1;
}

On Feb 4, 2007, at 6:23 AM, Darius Blaszijk wrote:

I understand what you mean. The problem however is that I don't
want to store the same data as table in the lua state and in my
application as object (for memory and debugging reasons). Therefore
I was thinking of using a getter and setter function that
encapsulates the data. So in effect:

data[1,2] = 1.234 is comparable in calling the function setdata
(1,2,1.234)
and print(data[1,2] is comparable to calling the function getdata(1,2)

the first does not return any value, the second returns the value
of the data object in my app at position 1,2

Darius

----- Original Message ----- From: "steffenj" <steffenj@arcor.de>
To: "'Lua list'" <lua@bazar2.conectiva.com.br>
Sent: Sunday, February 04, 2007 12:06 PM
Subject: AW: manipulating data


i suppose you want something like this:

matrix =
{
[1] = {1,2,3,4},
[2] = {2,3,4,5},
[3] = {3,4,5,6},
[4] = {4,5,6,7},
}

access it like this:

print(matrix[1][2])
2


in short, matrix is an (array) table consisting of (array) tables
array table == table indexed by numbers only


-----Ursprüngliche Nachricht-----
Von: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] Im Auftrag von Darius
Blaszijk
Gesendet: Sonntag, 4. Februar 2007 10:52
An: Lua list
Betreff: manipulating data

In my application I have a matrix of data for which I want to use
Lua to
manipulate it. I'm planning to implement filtering functions,
statistical
functions and the like.
What I'm still struggeling with though is how to represent the data
in Lua.
Currently I'm planning to keep the data in my app and provide
getters and
setters for the data. I'm finding difficulties though to find a proper
description of it in the PIL or refman. But afair there should be a
possibility to do it.

So what I want to be able to do is:

data[1,2] = 1.234
print(data[1,2])  -- returns 1.234

Can someone point me to the right resources so I can start reading and
testing myself? Or perhaps someone can give me a hint.

TIA Darius




--
Gé Weijers
ge@weijers.org