victormgons Posted October 25, 2016 Share Posted October 25, 2016 Hi, i have this script that isn't working correctly. testTable = { ["1"] = {}, ["2"] = {}, ["3"] = {}, ["4"] = {}, ["5"] = {}, ["6"] = {}, ["7"] = {}, ["8"] = {}, ["9"] = {}, ["10"] = {}, } for catName,category in pairs(testTable)do outputChatBox(catName) end It doesn't show the numbers in order, it shows: 1 3 2 5 4 7 6 9 8 10. What's wrong? Is there a way to fix it? Thanks Link to comment
Captain Cody Posted October 25, 2016 Share Posted October 25, 2016 Use ipairs instead of pairs. Link to comment
Walid Posted October 26, 2016 Share Posted October 26, 2016 * ipairs: loop only number indexes "start from 1 index" and only in order. * pairs: Loop all. Link to comment
victormgons Posted October 26, 2016 Author Share Posted October 26, 2016 I tried to use ipairs but it didn't show nothing Link to comment
Captain Cody Posted October 26, 2016 Share Posted October 26, 2016 Oh whoops I'm sorry I did not read that correctly, remove the "" around the numbers, instead of "1" use 1. testTable = { [1] = {}, [2] = {}, [3] = {}, [4] = {}, [5] = {}, [6] = {}, [7] = {}, [8] = {}, [9] = {}, [10] = {}, } for catName,category in pairs(testTable)do outputChatBox(catName) end Link to comment
Simple0x47 Posted October 26, 2016 Share Posted October 26, 2016 This is the way to do it. testTable = { {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, } for i = 1, #testTable do local catName = i local category = testTable[ i ] outputChatBox(catName) end Link to comment
Simple0x47 Posted October 26, 2016 Share Posted October 26, 2016 The one that I used does that automatically with no-need of using table.sort . Numeric for life :v Link to comment
victormgons Posted October 29, 2016 Author Share Posted October 29, 2016 But i'm using strings, i posted with numbers for a example Link to comment
victormgons Posted October 29, 2016 Author Share Posted October 29, 2016 I'm using the table like this testTable = { ["Primary Weapon"] = {}, ["Secondary Weapon"] = {}, ["Pistols"] = {}, ["Specially Weapon"] = {}, ["Ammo"] = {}, ["Food"] = {}, ["Medic Stuff"] = {}, ["Vehicle Parts"] = {}, ["Items"] = {}, ["Toolbelt"] = {}, } for catName,category in pairs(testTable)do outputChatBox(catName) end And it needs to start on Primary Weapon and finish on Toolbelt, in order.. Link to comment
Captain Cody Posted October 29, 2016 Share Posted October 29, 2016 (edited) Here testTable = { {"Primary Weapon"}, {"Secondary Weapon"}, {"Pistols"}, {"Specially Weapon"}, {"Ammo"}, {"Food"}, {"Medic Stuff"}, {"Vehicle Parts"}, {"Items"}, {"Toolbelt"}, } for i = 1, #testTable do outputChatBox(testTable[i][1]) end Edited October 29, 2016 by CodyL Link to comment
KariiiM Posted October 29, 2016 Share Posted October 29, 2016 Victor, can I know why you creating empty tables inside a table ? what's the purpose of that Link to comment
Walid Posted October 29, 2016 Share Posted October 29, 2016 i don't know what you want exactly, maybe you need sth like this local testTable = { ["Primary Weapon"] = 1, ["Secondary Weapon"] = 2, ["Pistols"] = 3, ["Specially Weapon"] = 4, ["Ammo"] = 5, ["Food"] = 6, ["Medic Stuff"] = 7, ["Vehicle Parts"] = 8, ["Items"] = 9, ["Toolbelt"] = 10, } function switch(theTable) local order = {} for k,v in pairs(theTable) do order[v] = k end return order end local newTable = switch(testTable) for i=1 ,#newTable do outputChatBox(i..": "..newTable[i]) end Link to comment
victormgons Posted October 29, 2016 Author Share Posted October 29, 2016 (edited) 1 hour ago, KariiiM said: Victor, can I know why you creating empty tables inside a table ? what's the purpose of that This is a part of an inventory, the names are tha categories and inside them will have items Edited October 29, 2016 by victormgons Quote Link to comment
KariiiM Posted October 29, 2016 Share Posted October 29, 2016 (edited) Victor, you can't do it with your case. @Gravestone, table.sort won't do what he wants to achieve, because this function will only do an alphabetical order. you can use Walid's way its good enough to do what you are looking for Edited October 29, 2016 by KariiiM Link to comment
GTX Posted October 29, 2016 Share Posted October 29, 2016 (edited) Lua does not retain the order that key, value pairs are added. However, this can be emulated. function orderedTable(...) local newTable,keys,values={},{},{} newTable.pairs=function(self) local count=0 return function() count=count+1 return keys[count],values[keys[count]] end end setmetatable(newTable,{ __newindex=function(self,key,value) if not self[key] then table.insert(keys,key) elseif value==nil then local count=1 while keys[count]~=key do count = count + 1 end table.remove(keys,count) end values[key]=value end, __index=function(self,key) return values[key] end }) for x=1,table.getn(arg) do for k,v in pairs(arg[x]) do newTable[k]=v end end return newTable end Example usage: test = orderedTable({value="key"},{anotherValue="anotherKey"}) test["apple"] = "red" test["mta"] = "sa" for k,v in test:pairs() do outputChatBox(k.." "..v) end The output is ordered; something like: Quote value key anotherValue anotherKey apple red mta sa Not tested Edited October 29, 2016 by GTX Link to comment
GTX Posted October 30, 2016 Share Posted October 30, 2016 Double post because can't edit heh... Anyways, here's updated code: function orderedTable(...) local newTable,keys,values={},{},{} newTable.pairs=function(self) local count=0 return function() count=count+1 return keys[count],values[keys[count]] end end setmetatable(newTable,{ __newindex=function(self,key,value) if not self[key] then table.insert(keys,key) elseif value==nil then local count=1 while keys[count]~=key do count = count+1 end table.remove(keys,count) end values[key] = value end, __index=function(self,key) return values[key] end }) return newTable end testTable = orderedTable() testTable["Primary Weapon"] = {} testTable["Secondary Weapon"] = {} testTable["Pistols"] = {} testTable["Specially Weapon"] = {} testTable["Ammo"] = {} testTable["Food"] = {} testTable["Medic Stuff"] = {} testTable["Vehicle Parts"] = {} testTable["Items"] = {} testTable["Toolbelt"] = {} for catName,category in testTable:pairs() do outputChatBox(catName) end It works, I tested it. When you add something to a table, __newindex is invoked and the value you passed is added to a indexed table. You can later fetch it. It also has when you set a key to nil, it will be removed from table, so if you'd like to remove Pistols key, all you need to do is: testTable["Pistols"] = nil. Link to comment
victormgons Posted October 31, 2016 Author Share Posted October 31, 2016 On 29/10/2016 at 11:17 PM, GTX said: Double post because can't edit heh... Anyways, here's updated code: function orderedTable(...) local newTable,keys,values={},{},{} newTable.pairs=function(self) local count=0 return function() count=count+1 return keys[count],values[keys[count]] end end setmetatable(newTable,{ __newindex=function(self,key,value) if not self[key] then table.insert(keys,key) elseif value==nil then local count=1 while keys[count]~=key do count = count+1 end table.remove(keys,count) end values[key] = value end, __index=function(self,key) return values[key] end }) return newTable end testTable = orderedTable() testTable["Primary Weapon"] = {} testTable["Secondary Weapon"] = {} testTable["Pistols"] = {} testTable["Specially Weapon"] = {} testTable["Ammo"] = {} testTable["Food"] = {} testTable["Medic Stuff"] = {} testTable["Vehicle Parts"] = {} testTable["Items"] = {} testTable["Toolbelt"] = {} for catName,category in testTable:pairs() do outputChatBox(catName) end It works, I tested it. When you add something to a table, __newindex is invoked and the value you passed is added to a indexed table. You can later fetch it. It also has when you set a key to nil, it will be removed from table, so if you'd like to remove Pistols key, all you need to do is: testTable["Pistols"] = nil. It worked, thanks a lot 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