[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: substitute missing keys?
- From: PA <petite.abeille@...>
- Date: Sun, 12 Mar 2006 20:06:27 +0100
Hello,
I would like to substitute missing keys with their string equivalent
(e.g. name -> 'name' ).
This is quiet straightforward for simple names, but gets a bit hairy
with compound names (e.g. name.id -> 'name.id' ).
Right now, I'm wrapping the missing keys in a custom table which
accumulates each name and concatenates them:
local function WrapperIndex( aTable, aKey )
aTable[ #aTable + 1 ] = tostring( aKey )
return aTable
end
local function WrapperToString( aTable )
return table.concat( aTable, "." )
end
local anIndex = function( aTable, aKey )
local aValue = _G[ aKey ]
if aValue == nil then
local aWrapper = { tostring( aKey ) }
local aMetaTable = { __index = WrapperIndex, __tostring
= WrapperToString }
setmetatable( aWrapper, aMetaTable )
aValue = aWrapper
end
return aValue
end
To complicate matters, those wrappers need to be post processed to
convert them to simple strings:
local function pack( ... )
local someArguments = { ... }
local aLength = select( "#", ... )
for anIndex = 1, aLength do
local aValue = select( anIndex, ... )
if type( aValue ) == "table" then
local aMetatable = getmetatable( aValue )
if aMetatable ~= nil and aMetatable.__tostring
== WrapperToString then
aValue = tostring( aValue )
someArguments[ anIndex ] = aValue
end
end
end
return someArguments, 1, aLength
end
All in all, quite convoluted :/
Is there a simpler way to achieve such substitutions?
Cheers
--
PA, Onnay Equitursay
http://alt.textdrive.com/