Jump to content

array indexing


typhoon
 Share

Recommended Posts

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
Link to comment
Share on other sites

  • Developers

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 .

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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" ;)

Link to comment
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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...