lua-users home
lua-l archive

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


Luiz Henrique de Figueiredo wrote:
example = {["foo"], ["bar"], ["baz"]}

as an abbreviation for

example = {["foo"] = true, ["bar"] = true, ["baz"] = true}

This hadn't come up until now, but a common idiom is to let Lua do the work:

function makeset(t)
	local s={}
	for i,v in ipairs(t) do
		s[v]=true
	end
	return s
end

example = makeset{"foo", "bar", "baz"}


I very fond of the following idiom:

function wordsToSet(str, rv)
  rv = rv or {}
  for k in str:gmatch"%S+" do rv[k] = k end
  return rv
end

example = wordsToSet[[foo bar baz]]