Memory Posted December 15, 2012 Share Posted December 15, 2012 Hi, please, help me sort players of points in xml. <users> <user> <SERIAL_XXXXXXXXXXXXXXXXXXXXXXXXXXX points="380" lastPlayerName="Player" money="6000" first="5" second="0" third="0"></SERIAL_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX> </user> </users> Link to comment
Guest Guest4401 Posted December 15, 2012 Share Posted December 15, 2012 You need to use xmlNodeGetChildren and table.sort. I remember helping someone with xml sorting, search for it, you will find the code. EDIT: Here it is: viewtopic.php?f=91&t=42259 Link to comment
Memory Posted December 15, 2012 Author Share Posted December 15, 2012 Thanks, but code have error and I can't fix it. Error ":20 atempt to concantenate field 'data' (a boolean value)" local amountOfTopsToDisplay = 3 -- how many tops you need addCommandHandler("top", function(player, command, value) local fileindex = xmlLoadFile("users.xml") -- load file local file = xmlNodeGetChildren(fileindex) if file then -- if file exists if value == "wins" or value == "points" or value == "second" or value == "third" then -- if syntax is right local tableOrder = {} -- make a table local subTable = {} -- make a sub table for i,v in ipairs(xmlNodeGetChildren(fileindex)) do -- get children from file subTable.name = xmlNodeGetAttribute(v, "nick") -- name goes in subTable subTable.data = xmlNodeGetAttribute(v, value) -- data goes in subTable table.insert(tableOrder, subTable) -- subTable is inserted in bigger table subTable = {} -- empty subTable for next loop end table.sort(tableOrder, function(a,b) return tonumber(a.data) > tonumber(b.data) end) -- put data in order outputChatBox("Displaying top "..value, player, 255,255,255,true) for i,v in ipairs(tableOrder) do -- loop the sorted table outputChatBox(i..": "..v.name.." - "..v.data, player, 255,255,255,true) -- display in order if i == amountOfTopsToDisplay then break end -- when enough tops has been shown, break it end else outputChatBox("Syntax : /top [wins|points|second|third]", player, 255,255,255,true) -- show the correct syntax if he got it wrong end xmlUnloadFile(fileindex) -- unload the file from memory end end ) Link to comment
Memory Posted December 16, 2012 Author Share Posted December 16, 2012 Help, please. I need in this function. Link to comment
boro Posted December 16, 2012 Share Posted December 16, 2012 try this local amountOfTopsToDisplay = 3 -- how many tops you need addCommandHandler("top", function(player, command, value) local fileindex = xmlLoadFile("users.xml") -- load file local file = xmlNodeGetChildren(fileindex) if file then -- if file exists if value == "wins" or value == "points" or value == "second" or value == "third" then -- if syntax is right local tableOrder = {} -- make a table local subTable = {} -- make a sub table for i,v in ipairs(xmlNodeGetChildren(fileindex)) do -- get children from file subTable.name = xmlNodeGetAttribute(v, "nick") -- name goes in subTable subTable.data = xmlNodeGetAttribute(v, value) -- data goes in subTable table.insert(tableOrder, subTable) -- subTable is inserted in bigger table subTable = {} -- empty subTable for next loop end table.sort(tableOrder, function(a,b) return tonumber(a.data) > tonumber(b.data) end) -- put data in order outputChatBox("Displaying top "..value, player, 255,255,255,true) for i,v in ipairs(tableOrder) do -- loop the sorted table outputChatBox(i..": "..v.name.." - "..a,b.data, player, 255,255,255,true) -- display in order if i == amountOfTopsToDisplay then break end -- when enough tops has been shown, break it end else outputChatBox("Syntax : /top [wins|points|second|third]", player, 255,255,255,true) -- show the correct syntax if he got it wrong end xmlUnloadFile(fileindex) -- unload the file from memory end end ) Link to comment
Memory Posted December 16, 2012 Author Share Posted December 16, 2012 I fix it. local amountOfTopsToDisplay = 3 -- how many tops you need addCommandHandler("top", function(player, command, value) fileindex = xmlLoadFile("users.xml") -- load file file = xmlFindChild ( fileindex, "user", 0 ) if file then -- if file exists if value == "wins" or value == "points" or value == "second" or value == "third" then -- if syntax is right local tableOrder = {} -- make a table local subTable = {} -- make a sub table for i,v in ipairs(xmlNodeGetChildren(file)) do -- get children from file subTable.name = xmlNodeGetAttribute(v, "Name") -- name goes in subTable subTable.data = xmlNodeGetAttribute(v, value) -- data goes in subTable table.insert(tableOrder, subTable) -- subTable is inserted in bigger table subTable = {} -- empty subTable for next loop end table.sort(tableOrder, function(a,b) return tonumber(a.data) > tonumber(b.data) end) -- put data in order outputChatBox("Displaying top "..value, player, 255,255,255,true) for i,v in ipairs(tableOrder) do -- loop the sorted table outputChatBox(i..": "..v.name.." - "..v.data, player, 255,255,255,true) -- display in order if i == amountOfTopsToDisplay then break end -- when enough tops has been shown, break it end else outputChatBox("Syntax : /top [wins|points|second|third]", player, 255,255,255,true) -- show the correct syntax if he got it wrong end xmlUnloadFile(fileindex) -- unload the file from memory end end ) Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now