Sign in to follow this  
Followers 0
typhoon

array indexing

6 posts in this topic

#1 ·  Posted (edited)

In testing, it seems that arrays are 0-based, but the examples in the docs seem to imply that they are 1-based. Can someone clarify?

Here's an example

dim $LABEL_TEXT[4] = ["left", "right", "top", "bottom"]
for $i = 1 to 4
    MsgBox(0, "note", $LABEL_TEXT[$i])
    GUICtrlCreateLabel($LABEL_TEXT[$i], 2 * ($i - 1) * $CELL_WIDTH, 0)
next

I am expecting it to iterate from [1] to [4] but it starts with the second value and ends with an out-of-bounds error. So I assume the arrays are actually 0-based despite the examples using [1] as the starting point?

Edited by typhoon

Share this post


Link to post
Share on other sites



In testing, it seems that arrays are 0-based, but the examples in the docs seem to imply that they are 1-based. Can someone clarify?

Here's an example

dim $LABEL_TEXT[4] = ["left", "right", "top", "bottom"]
for $i = 1 to 4
    MsgBox(0, "note", $LABEL_TEXT[$i])
    GUICtrlCreateLabel($LABEL_TEXT[$i], 2 * ($i - 1) * $CELL_WIDTH, 0)
next

I am expecting it to iterate from [1] to [4] but it starts with the second value and ends with an out-of-bounds error. So I assume the arrays are actually 0-based despite the examples using [1] as the starting point?

Arrays are always zero-based... so your example goes from 0 to 3 ...

Some examples are based on StringSplit() which createss a zero based array. position 0 hold the number of items found and the first Item is put in position 1 .


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Where did you found the info of 1-based?

Share this post


Link to post
Share on other sites

In the Help file, under Variables, all of the Array examples begin iteration at [1].

Share this post


Link to post
Share on other sites

In the Help file, under Variables, all of the Array examples begin iteration at [1].

Thanks,

But the declaration of the $array is not mention so you cannot conclude the 1-based assertion.

You have to look in the DIM/Local/Local definition.

AnyWay I will update the Variable page to avoid such "deduction" ;)

Share this post


Link to post
Share on other sites

Good catch @Typhoon,

At last someone really reading the help file ;)

I have noticed before that arrays can be a problem area for a new scripter so I have created a wiki page called Arrays. I will try to fill in some content during the day.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0