Thread Tools Display Modes
07/21/14, 12:08 PM   #1
zgrssd
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 280
ipairs bugged?

As far as I understand the only difference between pairs and ipairs should be that ipairs goes by alphabetical/numerical order. Both should still go over all the data.

But when given a table like this:
[1] = "CHAT_CATEGORY_SAY"
[2] = "CHAT_CATEGORY_YELL"
[3] = "CHAT_CATEGORY_WHISPER_INCOMING"
[4] = "CHAT_CATEGORY_WHISPER_OUTGOING"
[6] = "CHAT_CATEGORY_ZONE"
... (the table is in total 41 entries long)

ipairs will stop dead after entry 4.
As opposed to pairs, wich goes properly over the full table.

As far as I understand the syntax those are global functions.
Any chance Zenimax bugged ipairs up?
Any chance some addon is overwriting ipairs with a faulty version?
  Reply With Quote
07/21/14, 12:11 PM   #2
Khaibit
 
Khaibit's Avatar
AddOn Author - Click to view addons
Join Date: Jun 2014
Posts: 26
Originally Posted by zgrssd View Post
As far as I understand the only difference between pairs and ipairs should be that ipairs goes by alphabetical/numerical order. Both should still go over all the data.

But when given a table like this:
[1] = "CHAT_CATEGORY_SAY"
[2] = "CHAT_CATEGORY_YELL"
[3] = "CHAT_CATEGORY_WHISPER_INCOMING"
[4] = "CHAT_CATEGORY_WHISPER_OUTGOING"
[6] = "CHAT_CATEGORY_ZONE"
... (the table is in total 41 entries long)

ipairs will stop dead after entry 4.
As opposed to pairs, wich goes properly over the full table.

As far as I understand the syntax those are global functions.
Any chance Zenimax bugged ipairs up?
Any chance some addon is overwriting ipairs with a faulty version?
From what I understand, ipairs only works with "well-formed" tables (i.e. sequential indices without any gaps, 1-indexed). Your table is missing index 5, which is why ipairs stops there.
  Reply With Quote
07/21/14, 12:22 PM   #3
zgrssd
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 280
Originally Posted by Khaibit View Post
From what I understand, ipairs only works with "well-formed" tables (i.e. sequential indices without any gaps, 1-indexed). Your table is missing index 5, which is why ipairs stops there.
Now that you mentioned it I looked up the definition. It appears like "ipairs" for really only is a short version of "for i=1, #table, 1 do":
http://www.luafaq.org/#T1.10

Looks like the source that originally pointed me to ipairs was not precise enough in representing what it cannot do.
  Reply With Quote
07/21/14, 01:54 PM   #4
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
Originally Posted by zgrssd View Post
Now that you mentioned it I looked up the definition. It appears like "ipairs" for really only is a short version of "for i=1, #table, 1 do":
http://www.luafaq.org/#T1.10
With the minor difference that ipairs doesn't even need to get #table. The generator it returns simply stops at the first nil element.
  Reply With Quote
07/21/14, 02:32 PM   #5
Xrystal
caritas omnia vincit
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 369
I always translated ipairs to mean indexedpairs so only ever use it for tables that I know were indexed 1 to #tab. For the most part I use pairs as most of my tables are indexed by name rather than a running counter.

If I use table.insert and I don't delete elements on the fly I use ipairs. Otherwise I would use pairs.
  Reply With Quote
07/22/14, 08:23 PM   #6
Deome
 
Deome's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 29
Thumbs up

Originally Posted by merlight View Post
With the minor difference that ipairs doesn't even need to get #table. The generator it returns simply stops at the first nil element.
Yep. My addon relies HEAVILY on tables, and tables within tables ad nauseum, and I think there's only a single ipairs() call in 800 or so lines of code. Could be wrong, maybe two. If your table contains, or may contain, or may contain sometime in the future, anything other than a pure sequential index that starts with 1, just use pairs() to cycle through the whole thing. Besides, you can do so much more with pairs() than ipairs().

Also, my new favorite ZO function is NonContiguousCount(table). I haven't seen it mentioned much, but it's basically #table for non-sequential or alphabetical tables, which is a gods-send for me.
  Reply With Quote
07/22/14, 10:54 PM   #7
katkat42
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 155
Originally Posted by Deome View Post
Also, my new favorite ZO function is NonContiguousCount(table). I haven't seen it mentioned much, but it's basically #table for non-sequential or alphabetical tables, which is a gods-send for me.
Really? And I've been using one I had rolled on my own!
  Reply With Quote
07/23/14, 03:17 PM   #8
Xrystal
caritas omnia vincit
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 369
Originally Posted by katkat42 View Post
Really? And I've been using one I had rolled on my own!
Woot, no more for loops rofl. Although that function probably does all that stuff itself anyway.
  Reply With Quote
07/23/14, 04:37 PM   #9
Sephiroth018
 
Sephiroth018's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 20
Originally Posted by Xrystal View Post
Woot, no more for loops rofl. Although that function probably does all that stuff itself anyway.
One less self made helper function for me too
  Reply With Quote

ESOUI » Developer Discussions » General Authoring Discussion » ipairs bugged?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off