lua-users home
lua-l archive

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

untested code:

function GetElement(table, field)
	local pos = strfind(field,".")
	if pos then 
		return GetElement(table[strsub(field,1,pos-1)], strsub(field,pos+1)) 
	return table[field]

> -----Original Message-----
> From:
> []On Behalf Of Curt Carpenter
> Sent: Tuesday, November 27, 2001 8:27 AM
> To: Multiple recipients of list
> Subject: Table element addressing syntax
> Is there any way to reference a nested element of a table given a string
> reference to it? That is, given some simple data like this:
> foo = {a=1, b=2, c={d=3}}
> Could you have a function like this (I know this function doesn't work):
> function GetElement(table, field)
> 	return table[field]
> end
> such that with the above data GetElement(foo, "c.d") returns 3?
> Thanks,
> Curt