Why use ipairs and not just #?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
noatom
Prole
Posts: 10
Joined: Sun Feb 17, 2013 9:51 pm

Why use ipairs and not just #?

Post by noatom »

So ipairs is mostly used like this:
for i,v in ipairs(t) do print(i,v) end

Why not just do this?
for i,#t do print(i,v) end
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Why use ipairs and not just #?

Post by Boolsheet »

That's not valid Lua. You would have to do this.

Code: Select all

for i = 1, #t do
    print(i, t[i])
end
It's mostly for convenience and readability.
Shallow indentations.
noatom
Prole
Posts: 10
Joined: Sun Feb 17, 2013 9:51 pm

Re: Why use ipairs and not just #?

Post by noatom »

what about:

Code: Select all

t = { apple="green", orange="orange", banana="yellow" }
for k,v in pairs(t) do print(k,v) end
is there an alternative to pairs? like above?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Why use ipairs and not just #?

Post by bartbes »

Short version: Not really.
Long version:

Code: Select all

do
  local k, v = next(t)
  while k do
    --stuff
    k, v = next(t, k)
  end
end
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Why use ipairs and not just #?

Post by Taehl »

Numeric for is faster than ipairs, so I'd suggest using it in time-critical code (ie., inside love.update and love.draw), but elsewhere, ipairs is fine.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Xgoff
Party member
Posts: 211
Joined: Fri Nov 19, 2010 4:20 am

Re: Why use ipairs and not just #?

Post by Xgoff »

plus, ipairs and a numeric loop using # behave differently: ipairs will stop at the first nil, # will stop at whichever nil it wants (the typical problem it has with holes)
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Why use ipairs and not just #?

Post by Taehl »

Are you sure about that, Xgoff? I use #table all the time, and have never had any behavior like that. I thought that ipairs always behaved exactly the same as #table in regards to sequential numeric indices and nils.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Why use ipairs and not just #?

Post by Boolsheet »

Yes, that is correct. The Lua manual states that the length operator can ignore such holes in the sequence. An example:

Code: Select all

print( #{1, nil, 3} ) -- May print 3
This can vary with the implementation. LuaJIT behaves differently here.
Shallow indentations.
User avatar
master both
Party member
Posts: 262
Joined: Tue Nov 08, 2011 12:39 am
Location: Chile

Re: Why use ipairs and not just #?

Post by master both »

maybe, he were talking about this type of holes .
ps:boolsheet, great web page.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 188 guests