lua-users home
lua-l archive

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



On Mar 01, 2006, at 22:17, Chris wrote:

Are there any tricks to insert all the keys and values of one table into
another?

For example:

t = {}
t.a = "a"
t.b = "b"
t.c = "c"

u = { unpack(t), d="d", e="e" }

I know you can't use unpack except on numbered index values. Is my only
choice a table.foreach with an insert of each value from `t' into `u'?

Ahhh... the lure of one liners... 8^)

local aMap = LUMap( { a = "a", b = "b", c = "c" } )
local aMapCopy = aMap:copy():putAll( { d = "d", e = "e" } )

print( "aMap", aMap )
print( "aMapCopy", aMapCopy )

> aMap     { a = a, c = c, b = b }
> aMapCopy { a = a, c = c, b = b, e = e, d = d }

And as others have pointed out, you will have to write your own code for this level of "convenience". Which is somewhat unfortunate as such operations are arguably quiet common.

Perhaps you might be interested by Reuben Thomas 'Standard Library Proposal':

http://lua-users.org/wiki/StandardLibraryProposal
http://lua-users.org/wiki/StandardLibraries

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/