lua-users home
lua-l archive

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


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.