• Table Sorting
    9 replies, posted
Hey guys. I got this table, then I print it on my derma menu, and it prints in a order that doesn't give any sense.. Any way to sort/fix/print it in the correct order? [lua] banlengths = { [60] = "1 hour", [1440] = "1 day", [4320] = "3 days", [10080] = "1 week", [0] = "Permanent" } for x, y in pairs(banlengths) do local BAN_SUBMENUREASON = BAN_SUBMENULENGTH:AddSubMenu(y) end [/lua] Result when I print it: [lua] ] lua_run_cl for k,v in pairs(banlengths) do print(k) end 0 1440 4320 60 10080 [/lua]
pairs is not guaranteed to iterate in the same order as the table has been defined in. Use ipairs of you need sorting.
I've tried ipairs, then nothing happened. I just get no return in lua_run or in the script.
How did you do it? Post code. Or try table.sort I can't quite remember the syntax at the moment, but i think it's something like [LUA]table.sort( your Table, function( a, b ) return a > b end )[/LUA] although you're going to want to double check that Itd be better to use ipairs anyway
If you want to use ipairs it has to be a hash table like this: [lua] local BAN_LEN_STRINGS = { {Len = 0, Str = "Permanent"} {Len = 60, Str = "1 Hour"} {Len = 1440, Str = "1 Day"} {Len = 4320, Str = "3 Days"} {Len = 10080, Str = "1 Week"} } print(BAN_LEN_STRINGS[1].Str) [/lua]
ipairs iterates over sequential entries only, aka there can be no gaps.
[QUOTE=rejax;44318766]How did you do it? Post code. Or try table.sort I can't quite remember the syntax at the moment, but i think it's something like [LUA]table.sort( your Table, function( a, b ) return a > b end )[/LUA] although you're going to want to double check that Itd be better to use ipairs anyway[/QUOTE] The table.sort function didn't seem to work for me. I used ipars like this: [lua] ] lua_run_cl for k,v in ipairs(banlengths) do print(k) end [/lua] [QUOTE=brandonj4;44318780]If you want to use ipairs it has to be a hash table like this: [lua] local BAN_LEN_STRINGS = { {Len = 0, Str = "Permanent"} {Len = 60, Str = "1 Hour"} {Len = 1440, Str = "1 Day"} {Len = 4320, Str = "3 Days"} {Len = 10080, Str = "1 Week"} } print(BAN_LEN_STRINGS[1].Str) [/lua][/QUOTE] Tried your code, but the table seems to create errors: [lua] [ERROR] addons/ulxqm/lua/autorun/cl_ulxqm.lua:23: '}' expected (to close '{' at line 21) near '{' 1. unknown - addons/ulxqm/lua/autoru [/lua] Line 21 is: {Len = 0, Str = "Permanent"}
[lua] local BAN_LEN_STRINGS = { {Len = 0, Str = "Permanent"}, {Len = 60, Str = "1 Hour"}, {Len = 1440, Str = "1 Day"}, {Len = 4320, Str = "3 Days"}, {Len = 10080, Str = "1 Week"} } [/lua]
[QUOTE=Blt950;44318980]The table.sort function didn't seem to work for me. I used ipars like this: [lua] ] lua_run_cl for k,v in ipairs(banlengths) do print(k) end [/lua] Tried your code, but the table seems to create errors: [lua] [ERROR] addons/ulxqm/lua/autorun/cl_ulxqm.lua:23: '}' expected (to close '{' at line 21) near '{' 1. unknown - addons/ulxqm/lua/autoru [/lua] Line 21 is: {Len = 0, Str = "Permanent"}[/QUOTE] Sorry 5am over here, forgot the ","
Great! This works :) Thanks!
Sorry, you need to Log In to post a reply to this thread.