Lua in pairs in the same order as written

Is there a way to iterate over a table like the one below in the same order in which it was written?

local tbl = {
    ["hello"] = 1,
    [2] = 2,
    [50] = 3,
    ["bye"] = 4,
    [200] = 5
}

I mean, when I use "in pairs", every time I execute my code, I get a different order ...

I am looking for something like this:

function get_keys(tbl)
    local rtable = {}
    for k,v in pairs(tbl) do
        table.insert(rtable, k)
    end
    return rtable
end

local keys_of_tbl = get_keys(tbl)
for i = 1, table.getn(keys_of_tbl) do
    --Do something with: tbl[keys_of_tbl[i]]
end

But since the get_keys function is based on "in pairs" again, it will not work ...

+4
source share
3 answers

Lua , , . , , ipairs ( ). , , .


EDIT ( )

__newindex, , . ordered_add , _keys _values. , __newindex , , "" _keys _values.

, , - , "_keys" , ordered_table.insert(t, key, value) ordered_table.index(t, key) ordered_table.remove(t, key).

ordered_table = {}

function ordered_table.insert(t, k, v)
  if not rawget(t._values, k) then -- new key 
    t._keys[#t._keys + 1] = k
  end
  if v == nil then -- delete key too.
    ordered_table.remove(t, k)
  else -- update/store value
    t._values[k] = v 
  end
end

local function find(t, value)
  for i,v in ipairs(t) do
    if v == value then
      return i
    end
  end
end  

function ordered_table.remove(t, k)
  local v = t._values[k]
  if v ~= nil then
    table.remove(t._keys, find(t._keys, k))
    t._values[k] = nil
  end
  return v
end

function ordered_table.index(t, k)
    return rawget(t._values, k)
end

function ordered_table.pairs(t)
  local i = 0
  return function()
    i = i + 1
    local key = t._keys[i]
    if key ~= nil then
      return key, t._values[key]
    end
  end
end

function ordered_table.new(init)
  init = init or {}
  local t = {_keys={}, _values={}}
  local n = #init
  if n % 2 ~= 0 then
    error"in ordered_table initialization: key is missing value"
  end
  for i=1,n/2 do
    local k = init[i * 2 - 1]
    local v = init[i * 2]
    if t._values[k] ~= nil then
      error("duplicate key:"..k)
    end
    t._keys[#t._keys + 1]  = k
    t._values[k] = v
  end
  return setmetatable(t,
    {__newindex=ordered_table.insert,
    __len=function(t) return #t._keys end,
    __pairs=ordered_table.pairs,
    __index=t._values
    })
end

--- Example Usage:
local t = ordered_table.new{
  "hello", 1,  -- key, value pairs
  2, 2,
  50, 3,
  "bye", 4,
  200, 5
}

print(#t)
print("hello is", t.hello)
print()
for k, v in pairs(t) do  --- Lua 5.2 __pairs metamethod
  print(k, v)
end
t.bye = nil -- delete that
t[2] = 7 -- use integer keys
print(#t) 
+4

. " " . (, .) Lua " " .

, - .

, ( ipairs ).

, , , , ipairs .

+5

, :

function switch_key_val(tbl)
    local rtbl = {}
    for k,v in pairs(tbl) do
        rtbl[v] = k
    end
    return rtbl
end

:

local tbl = {
    ["hello"] = 1,
    [2] = 2,
    [50] = 3,
    ["bye"] = 4,
    [200] = 5
}
local tbl = switch_key_val(tbl)

for v,k in pairs(tbl) do
    print(k,v)
end

which gives me the same result every time I execute my code

0
source

All Articles