[MudWalker] Help With Tables
Kevin Reid
kpreid
Thu Feb 26 18:30:30 PST 2004
Blastron <blastron at mac.com> wrote:
> Is it possible to sort one table, say table blah, by another table,
> table foo, so that items in table blah would appear in the order that
> they appear in table foo? For instance, if table blah contained A, B,
> D, and F, and table foo contained F, A, E, B, D, and C (in that order),
> whatever function would do that would sort table blah as F, A, B, D.
You would likely get better help with this problem by asking in a Lua
mailing list or other such forum. I'm not particularly familiar with the
language.
<http://www.lua.org/>
<http://www.lua.org/manual/5.0/>
<http://www.lua.org/community.html>
Here's what I came up with after a glance through the manual (you'll
have to add @@ to use this within MudWalker, of course):
local values = {"gamma", "alpha", "epsilon", "delta", "beta"}
local order = {"alpha", "beta", "gamma", "delta", "epsilon"}
function byorder(a, b)
local aIndex, bIndex
for orderIndex, orderValue in ipairs(order) do
if orderValue == a then
aIndex = orderIndex
end
if orderValue == b then
bIndex = orderIndex
end
if ai ~= nil and bi ~= nil then
return aIndex < bIndex
end
end
return a < b
end
table.sort(values, byorder)
for k, v in pairs(values) do
print(k, v)
end
> Also, is there a function to see if a particular value exists within a
> table that would return true or false, or, better yet, the location of
> the value within the table or false?
If you find one, let me know. The loop in the byorder() function above
exists only to do that search.
--
Kevin Reid
More information about the MudWalker
mailing list