lua-users home
lua-l archive

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


I missed! The metatable must be set to the result table as well. Or, thinking better, the collections don't need to be weak. :-)

Thank you,
Milano

--

local t1 = {} -- collection t1
t1[obj1] = true -- insert obj1 into collection t1
t1[obj2] = true -- insert obj2 into collection t1

local t2 = {} -- collection t2
t2[obj2] = true -- insert obj2 into collection t2

function subtract(t1, t2)
	local r = {}

	for k, v in pairs(t1) do
		if not t2[k] then
			r[k] = v
		end
	end

	return r
end

From: Mark Hamburg <mhamburg@adobe.com>
Reply-To: Lua list <lua@bazar2.conectiva.com.br>
To: Lua list <lua@bazar2.conectiva.com.br>
Subject: Re: LeftJoins with tables (removing values from tables)
Date: Mon, 27 Sep 2004 10:09:15 -0700

Why a weak-table for the collections or why not a weak table for the results
of the subtraction? Is this just the "an e-mail editor is not really a code
editor" issue?

Mark

on 9/25/04 10:45 AM, Milano Carvalho at milanogc@hotmail.com wrote:

> You would store the collection's objects as keys of a table.
> The values relative to each key would be used to store whatever you want.
>
> For example:
>
> local mt = {__mode = "k"}
>
> local t1 = setmetatable({}, mt) -- collection t1
> t1[obj1] = true -- insert obj1 into collection t1
> t1[obj2] = true -- insert obj2 into collection t1
>
> local t2 = setmetatable({}, mt) -- collection t2
> t2[obj2] = true -- insert obj2 into collection t2
>
> So the subtract function would be for example:
>
> function subtract(t1, t2)
> local r = {}
>
> for k, v in pairs(t1) do
> if not t2[k] then
> r[k] = v
> end
> end
>
> return r
> end
>
> Milano


_________________________________________________________________
MSN Messenger: converse com os seus amigos online. http://messenger.msn.com.br