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
Why use ipairs and not just #?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Why use ipairs and not just #?
That's not valid Lua. You would have to do this.
It's mostly for convenience and readability.
Code: Select all
for i = 1, #t do
print(i, t[i])
end
Shallow indentations.
Re: Why use ipairs and not just #?
what about:
is there an alternative to pairs? like above?
Code: Select all
t = { apple="green", orange="orange", banana="yellow" }
for k,v in pairs(t) do print(k,v) end
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Why use ipairs and not just #?
Short version: Not really.
Long version:
Long version:
Code: Select all
do
local k, v = next(t)
while k do
--stuff
k, v = next(t, k)
end
end
- 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 #?
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+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Re: Why use ipairs and not just #?
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)
- 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 #?
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+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Re: Why use ipairs and not just #?
Yes, that is correct. The Lua manual states that the length operator can ignore such holes in the sequence. An example:
This can vary with the implementation. LuaJIT behaves differently here.
Code: Select all
print( #{1, nil, 3} ) -- May print 3
Shallow indentations.
- master both
- Party member
- Posts: 262
- Joined: Tue Nov 08, 2011 12:39 am
- Location: Chile
Re: Why use ipairs and not just #?
maybe, he were talking about this type of holes .
ps:boolsheet, great web page.
ps:boolsheet, great web page.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 12 guests