lua-users home
lua-l archive

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


On Thu, Mar 28, 2013 at 11:49 AM, Meer H. van der <H.vanderMeer@uva.nl> wrote:
> Now the problem: I would like to be able to use in expressions
>   Container[1].extrafield
> where extrafield is the result of an operation to be performed on the fields of the data.
> For example, if field1 is a string, then extrafield could deliver string.sub(field1,1,2).

This can be done with metatatables. If a field is not found, then the
'metamethod' __index fires, which can do any computation.

t = {field1 == 'boohoo'}
setmetatatable(t, {
  __index = function (self,k)
     if k == 'extrafield' then
       return string.sub(t.field1,1,2)
    elseif ... -- and so on....
    end
  end
})

print(t.extrafield) --> bo

steve d.