Reputation: 385
Lets say that I have a dictionary in Lua (i.e. a Lua table that contains indexes of type string) like so:
local my_dictionary = {a = 123; b = 321; c = 456; d = 654};
What I am trying to do is create an iterator function that can iterate over a table even if its indexes are of type string; kind of like pairs, however whenever I try to call next() to get the next index,value it will only return the index,value if the index is of type int. An idea I had was maybe to call (index):byte(1, -1) and add up the tuple of ints, and use that as a sort of pretend index, just to keep track of the indexes, but I do not think that would work with next. Here is basically what I have so far:
local function each(list)
if #list > 0 then
local function my_itr(lst, ind)
return next(lst, ind);
end
return my_itr, List, 0;
end
return function() end, nil, nil;
end
this only works for a table with int indexes (an array table), so I was wondering if anyone could help me out. Thanks. Edit: To make this less vague here is an example piece of code of what I am trying to accomplish:
local mytable = {a = 123; b = 321; 3, 2, 1, c = "bca"};
for i,v in each(mytable) do
print(i,v);
end
what it should output:
>a 123
>b 321
>1 3
>2 2
>3 1
>c bca
The output would not have to be in exact order.
Upvotes: 1
Views: 1865
Reputation: 37
I took a swing at this problem for kicks, and came up with a solution that I hope can save someone a lot of time one day.
I found using a custom generator function did the trick:
--- A generator function for traversing a table in lexicographical key order.
---
--- Returns an iterator function that generates the `key`, `value`, and `index` of each row in the table.
---
--- @generic K,V
--- @param t table<K,V> Table to iterate over
--- @return fun(): key:K, value:V, index:integer
local function apairs(t)
local index = 0
-- Get Keys
local keys = {}
for key, _ in pairs(t) do table.insert(keys, key) end
-- Sort keys lexicographically (converting to string to handle mixed types)
table.sort(keys, function(a, b) return tostring(a) < tostring(b) end)
--- Iterator function.
--- @generic K,V
--- @return K,V,integer
return function()
index = index + 1
local key = keys[index]
return key, t[key], index
end
end
What it might look like in the wild:
-- Setting the test data
local actionResources = {
WarlockSpellSlot = { Hidden = "False", UUID = "e9127b70-22b7-42a1-b172-d02f828f260a", ReplenishType = "ShortRest" },
Interupt_Shield_MindFlayer = { Hidden = "True" , UUID = "7bd0ceb4-9191-4208-b924-909897948fb6", ReplenishType = "Rest" },
MagicStones = { Hidden = "True" , UUID = "c12836e6-0f4e-44a2-b84d-da7b1bcc9986", ReplenishType = "Never" },
Interrupt_EntropicWard_Charge = { Hidden = "True" , UUID = "89c063f2-dadf-49e4-830b-ceb9f50f3538", ReplenishType = "ShortRest" },
Interrupt_Portent_7 = { Hidden = "True" , UUID = "504b4d64-5832-45f7-b09a-ba994b0939ce", ReplenishType = "NA" },
ReactionActionPoint = { Hidden = "False", UUID = "45ff0f48-b210-4024-972f-b64a44dc6985", ReplenishType = "NA" },
BreathoftheDragonUse = { Hidden = "False", UUID = "ddadea14-74b6-4da9-b9da-b5e5b00c0547", ReplenishType = "Rest" },
Interrupt_Portent_18 = { Hidden = "True" , UUID = "1d766f02-04e4-4086-9b75-cb0621130e06", ReplenishType = "NA" },
FungalInfestationCharge = { Hidden = "False", UUID = "281f289d-3c0b-433a-a7ed-0505be88ec9e", ReplenishType = "Rest" },
SorceryPoint = { Hidden = "False", UUID = "46886ba5-6505-4875-a747-ac14118e1e08", ReplenishType = "Rest" },
BardicInspiration = { Hidden = "False", UUID = "46bbeb43-9973-40fb-a11f-e386bc425a8e", ReplenishType = "Rest" },
Interrupt_Portent_19 = { Hidden = "True" , UUID = "32dc0c71-b26e-482c-b439-4b261823373f", ReplenishType = "NA" },
WarPriestActionPoint = { Hidden = "False", UUID = "30cca4c5-a808-4c96-bd1c-f57bbb92dc1d", ReplenishType = "Rest" },
SteadyAimUse = { Hidden = "False", UUID = "f187bd93-f245-4d8d-b2c1-7e97c5ca831e", ReplenishType = "Rest" },
AncestralBoon = { Hidden = "False", UUID = "f3881182-64bc-415f-ad18-b0ac8b09d17b", ReplenishType = "Rest" },
ArcaneRecoveryPoint = { Hidden = "False", UUID = "74737a08-7a77-457b-9740-ae363be2b80f", ReplenishType = "Rest" },
InfectiousInspiration = { Hidden = "False", UUID = "a7795e7c-66bc-4cc5-a58e-2630434ab238", ReplenishType = "Rest" },
Interrupt_Portent_2 = { Hidden = "True" , UUID = "12cd6971-2efd-4780-b7a0-0cdba87045b9", ReplenishType = "NA" },
TidesOfChaos = { Hidden = "False", UUID = "733e3365-082c-4a0a-8df9-98273c23186e", ReplenishType = "ShortRest" },
VigilantDefenderReaction = { Hidden = "False", UUID = "7301fe0b-148f-4be9-8ab6-1e3b37a38ae1", ReplenishType = "NA" },
ActionPoint = { Hidden = "False", UUID = "734cbcfb-8922-4b6d-8330-b2a7e4c14b6a", ReplenishType = "NA" }
}
-- The obligatory aesthetics step, which aligns the columns so I can sleep at night
local w = 0; for x in pairs(actionResources) do w = math.max(w, #x) end
local fmt = "\t%-"..w.."s\t%s\t%2d"
-- And for the grand finally:
for key, value, index in apairs(actionResources) do
print(string.format(fmt, key, value, index))
end
The output then looks like this:
ActionPoint table: 0x55e64cc2a950 1
AncestralBoon table: 0x55e64cc2a530 2
ArcaneRecoveryPoint table: 0x55e64cc2a5e0 3
BardicInspiration table: 0x55e64cc2a270 4
BreathoftheDragonUse table: 0x55e64cc29fb0 5
FungalInfestationCharge table: 0x55e64cc2a110 6
InfectiousInspiration table: 0x55e64cc2a690 7
Interrupt_EntropicWard_Charge table: 0x55e64cc29da0 8
Interrupt_Portent_18 table: 0x55e64cc2a060 9
Interrupt_Portent_19 table: 0x55e64cc2a320 10
Interrupt_Portent_2 table: 0x55e64cc2a740 11
Interrupt_Portent_7 table: 0x55e64cc29e50 12
Interupt_Shield_MindFlayer table: 0x55e64cc2c740 13
MagicStones table: 0x55e64cc2c780 14
ReactionActionPoint table: 0x55e64cc29f00 15
SorceryPoint table: 0x55e64cc2a1c0 16
SteadyAimUse table: 0x55e64cc2a480 17
TidesOfChaos table: 0x55e64cc2a7f0 18
VigilantDefenderReaction table: 0x55e64cc2a8a0 19
WarPriestActionPoint table: 0x55e64cc2a3d0 20
WarlockSpellSlot table: 0x55e64cc2c700 21
As a bonus, here's a really short version for any heathens who don't document their code or hate type completion:
--- A generator function that returns an iterator that traverses a table in alphabetical key order.
local function apairs(t)
local keys, index = {}, 0
for k in pairs(t) do keys[#keys+1] = k end
table.sort(keys, function(a, b) return tostring(a) < tostring(b) end)
return function() index = index + 1; return keys[index], t[keys[index]], index end
end
And here's a one-liner version in-case you hate your coworkers are allergic to readability: (please don't use this)
local function apairs(t) local k,i={},0 for x in pairs(t) do k[#k+1]=x end table.sort(k,function(a,b) return tostring(a)<tostring(b) end) return function() i=i+1 return k[i],t[k[i]],i end end
Upvotes: 0
Reputation: 26794
It should work exactly as you want it to with a couple of tweaks: fix typo in List
and pass nil
instead of 0
:
local function each(list)
local function my_itr(lst, ind)
return next(lst, ind)
end
return my_itr, list, nil
end
local mytable = {a = 123; b = 321; 3, 2, 1, c = "bca"}
for i,v in each(mytable) do
print(i,v)
end
This prints the following for me, which is what you'd need:
1 3
2 2
3 1
a 123
b 321
c bca
Upvotes: 3
Reputation: 3592
You can achieve this very behavior by using pairs
. Don't confuse it with ipairs
though - these are two different table traversal functions!
While ipairs
only traverses integer keys of the table (usually it also stops at the first non-existent integer key), pairs
traverses all key-value pairs in the table.
So, by writing
local mytable = {a = 123; b = 321; 3, 2, 1, c = "bca"};
for i, v in pairs(mytable) do
print(i, v);
end
You'll get all key-value pairs printed, in some random order. Here's the demo.
As a sidenote, there's no such thing as 'dictionary' in Lua - all associative arrays are referred to as 'tables'.
Upvotes: 2