Jump to content

[Help] Loop pairs and tables


victormgons

Recommended Posts

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

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

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

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 by KariiiM
Link to comment

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 by GTX
Link to comment

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
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 :D

Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...