Reputation: 586
local sometable = {a = "ag", b = "fa"}
for k, v in ipairs(sometable) do
print(k, v)
end
The code above is my effort, so how do i print a, b in that table?!
Upvotes: 1
Views: 60
Reputation: 2161
ipairs
only traversers the array part of a table. What you can do is simply writing
print(sometable.a, sometable.b)
or you can cycle through both the dictionary and array parts of the table by using
for key, value in pairs(sometable)
You could also define your own iterator to only cycle through the dictionary part of the table. In my mind it would look like
function cycle(dict)
local contentarray = {}
for k, v in pairs(dict) do
contentarray[#contentarray + 1] = {k, v}
end
local n = 0
return function()
n = n + 1
if not contentarray[n] then
return
else
while type(contentarray[n][1]) ~= "string" do
n = n + 1
end
return contentarray[n][1], contentarray[n][2]
end
end
end
But that would be higly inefficient.
Upvotes: 0
Reputation: 122493
You are using the wrong iterator. ipairs
is for sequences. For hash-like tables, use pairs
instead:
for k, v in pairs(sometable) do
Upvotes: 2