PtokaX forum

Archive => Archived 4.0 boards => Request for Lua 4 scripts => Topic started by: bastya_elvtars on 13 September, 2005, 21:39:40

Title:
Post by: bastya_elvtars on 13 September, 2005, 21:39:40
That's not an array. An array is:

{ "a", "b", "c" }

See this page (http://ptxwiki.psycho-chihuahua.net/doku.php/scriptinghelp/tables).
Title:
Post by: NemeziS on 13 September, 2005, 22:11:10
Thanx, bastya_elvtars!

Now I know that it is a table.  :)

So, I need to know how to sort it by value (from the smallest to the biggest - like tables's order).

Need variant in lua4 and lua5.

Best regards,
NemeziS
Title:
Post by: bastya_elvtars on 13 September, 2005, 22:47:51
You have keys and values in a table. Let's just see!

tab={["a"]=1,["b"]=2}

Now what to do in order to display it sorted? This way even works if you don't know the contents:
-- lua4
arr={}
for a,b in tab do
tinsert(arr,a)
end
sort(arr)
for k,v in arr do
print(tab[v])
end


-- lua5
for a,b in pairs(table) do
table.insert(arr,a)
end
table.sort(arr)
for k,v in ipairs(arr) do
print(tab[v])
end

Hope this helps.