Table creation on demand for host application integration.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Table creation on demand for host application integration.

Greg McCreath
Hi all,

I'm looking to find an elegant way around the lua:

a = {}

a.b.c = "x";

where 'b' must exist before 'c' can be assigned.  I wanted to create 'b'
on the fly.  Something like:

a = {};
mt = {};
setmetatable(a, mt)

mt.__newindex = function (table, key)
  return {};
end

a.b.c = 10;  -- this should create 'b'
print(a.b.c)


... which, of course, blows up.  But I want 'b' to be created and
assigned to 'a' when it is asked for.

Why?  Well 'a' represents an external list of structures in the host
application.  Let's call them 'products'.  I'd like to represent these
products and their attributes to lua script as a table.  Nice and
elegant.  Such that I can do:

a.product100.qty = 10;

so that when the 'product100' key is not found a table can be supplied
with meta methods that can manipulate the external host-application
fields of that given product.

Some of the attributes of products are themselves structures.  Expand
this example to:

a.product100.barcode.type = "EAN13"

and so on.  None of my crazy experiments have managed to get this to
work ...

Many thanks, all help appreciated.

Greg.




This email and any files transmitted with it may be confidential and are intended solely for the use of the individual or entity to whom they are addressed. This email may contain personal information of individuals, and be subject to Commonwealth and/or State privacy laws in Australia. This email is also subject to copyright. If you are not the intended recipient, you must not read, print, store, copy, forward or use this email for any reason, in accordance with privacy and copyright laws. If you have received this email in error, please notify the sender by return email, and delete this email from your inbox. 


Reply | Threaded
Open this post in threaded view
|

Re: Table creation on demand for host application integration.

Luiz Henrique de Figueiredo
Reply | Threaded
Open this post in threaded view
|

RE: Table creation on demand for host application integration.

Greg McCreath
In reply to this post by Greg McCreath
Nice.  Thanks.  I hadn't seen that.  I had been looking though.

Cheers.  And thanks for the AMAZINGLY quick response.

Greg.

-----Original Message-----
From: Luiz Henrique de Figueiredo [[hidden email]] 
Sent: Thursday, 18 January 2007 12:32 PM
To: Lua list
Subject: Re: Table creation on demand for host application integration.

Have you seen http://lua-users.org/wiki/AutomagicTables ?



This email and any files transmitted with it may be confidential and are intended solely for the use of the individual or entity to whom they are addressed. This email may contain personal information of individuals, and be subject to Commonwealth and/or State privacy laws in Australia. This email is also subject to copyright. If you are not the intended recipient, you must not read, print, store, copy, forward or use this email for any reason, in accordance with privacy and copyright laws. If you have received this email in error, please notify the sender by return email, and delete this email from your inbox. 


Reply | Threaded
Open this post in threaded view
|

Re: Table creation on demand for host application integration.

Doug Rogers-4
Greg McCreath wrote:
>> Have you seen http://lua-users.org/wiki/AutomagicTables ?
> Nice.  Thanks.  I hadn't seen that.  I had been looking though.

I've attached a module-ized version of the code on the wiki page. Just
put it in your package.path and use:

require'autotable'
a = autotable.new()
a.b.c.d = "a.b and a.b.c are automatically created"

I found it useful. Hope it helps someone else.

Doug

-- 
--__-__-____------_--_-_-_-___-___-____-_--_-___--____
Doug Rogers - ICI - V:703.893.2007x220 www.innocon.com
-_-_--_------____-_-_-___-_--___-_-___-_-_---_--_-__-_
-- Based on code at http://lua-users.org/wiki/AutomagicTables, by Rici Lake
-- and Thomas Wrensch. Module by Doug Rogers. This code is in the public
-- domain.

-- Some further words from that site:

-- Building on some of the techniques illustrated in FuncTables, here are
-- some implementations of Perl-style automagic tables. An automagic table
-- creates subtables on demand, as it were; i.e.:

-- require'autotable'
-- a = autotable.new()
-- a.b.c.d = "a.b and a.b.c are automatically created"

local getmetatable = getmetatable
local setmetatable = setmetatable

module("autotable")

local auto, assign

function auto(tab, key)
  return setmetatable({}, {
          __index = auto,
          __newindex = assign,
          parent = tab,
          key = key
  })
end

local meta = {__index = auto}

-- The if statement below prevents the table from being
-- created if the value assigned is nil. This is, I think,
-- technically correct but it might be desirable to use
-- assignment to nil to force a table into existence.

function assign(tab, key, val)
-- if val ~= nil then
  local oldmt = getmetatable(tab)
  oldmt.parent[oldmt.key] = tab
  setmetatable(tab, meta)
  tab[key] = val
-- end
end

-- Creates and returns a new table with automagic properties.
function new()
  return setmetatable({}, meta)
end