Why does the output of "for in pairs" is right whereas the output of "for in ipairs" is wrong?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Why does the output of "for in pairs" is right whereas the output of "for in ipairs" is wrong?

孙世龙 sunshilong
Hi, list

Here are the related code snippets(which are contained in a file named
as test.lua):
local mt = {}
local Set = {}

function Set.new (l) -- 2nd version
local set = {}
setmetatable(set, mt)
for _, v in ipairs(l) do set[v] = true end
return set
end


function Set.union (a, b)
local res = Set.new{}
print(#res)
for k in pairs(a) do res[k] = true end
for k in pairs(b) do res[k] = true end
return res
end

-- presents a set as a string
function Set.tostring (set)
local l = {} -- list to put all elements from the set
for e in pairs(set) do
l[#l + 1] = tostring(e)
end
return "{" .. table.concat(l, ", ") .. "}"
end

s1 = Set.new{10, 20, 30, 50}
s2 = Set.new{30, 1}
print(s1[10],s1[20],s1[30],s1[50])
print(s1[1],s1[2],s1[3])
print(#s1)

print("\noutput is ok below")
--output is ok
for k, v in pairs(s1) do
print(k, v)
end


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

print("\noutput is strange below, why?")
--output is strange, why
for k, v in ipairs(s1) do
print(k, v)         --output nothing
end

print("---------------")
for k, v in ipairs(s2) do
print(k, v)          --only outputs the last elment
end

Here is the outputs while run the script(i.e. lua test.lua) on Lua-5.3.5 :

true    true    true    true
nil     nil     nil
0

output is ok below
20      true
50      true
10      true
30      true
1       true
30      true

output is strange below, why?
---------------
1       true

Why does the output of "for in pairs" is right whereas the output of
"for in ipairs" is wrong?

I have thought and thought about it, but I am still confused now.
I would be grateful to have some help with help with this question.

Best regards
Sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: Why does the output of "for in pairs" is right whereas the output of "for in ipairs" is wrong?

Robert Burke
On Thu, Oct 15, 2020 at 6:11 PM 孙世龙 sunshilong <[hidden email]> wrote:
> Why does the output of "for in pairs" is right whereas the output of
> "for in ipairs" is wrong?

The question is "Why, when I give ipairs a table that has a nil value
at t[2], does it stop iterating? And why, when I give it a table that
has a nil value at t[1], does it not even iterate over one entry?"

The manual says:
ipairs (t)
Returns three values (an iterator function, the table t, and 0) so
that the construction
     for i,v in ipairs(t) do body end
will iterate over the key–value pairs (1,t[1]), (2,t[2]), ..., up to
the first nil value.
https://www.lua.org/manual/5.3/manual.html#2.2

It is often helpful to read the manual. Lua is a small language and
the manual is small as well.
Reply | Threaded
Open this post in threaded view
|

Re: Why does the output of "for in pairs" is right whereas the output of "for in ipairs" is wrong?

孙世龙 sunshilong
>It is often helpful to read the manual. Lua is a small language and
>the manual is small as well.
Sorry for disturbing you.
I have studied the differences between "ipairs" and "pairs".
But I totally forgot them when I saw such outputs.

I will do more work before asking for help later.

Best regards
Sunshilong

On Thu, Oct 15, 2020 at 5:43 PM Robert Burke <[hidden email]> wrote:

>
> On Thu, Oct 15, 2020 at 6:11 PM 孙世龙 sunshilong <[hidden email]> wrote:
> > Why does the output of "for in pairs" is right whereas the output of
> > "for in ipairs" is wrong?
>
> The question is "Why, when I give ipairs a table that has a nil value
> at t[2], does it stop iterating? And why, when I give it a table that
> has a nil value at t[1], does it not even iterate over one entry?"
>
> The manual says:
> ipairs (t)
> Returns three values (an iterator function, the table t, and 0) so
> that the construction
>      for i,v in ipairs(t) do body end
> will iterate over the key–value pairs (1,t[1]), (2,t[2]), ..., up to
> the first nil value.
> https://www.lua.org/manual/5.3/manual.html#2.2
>
> It is often helpful to read the manual. Lua is a small language and
> the manual is small as well.