Jump to content

Logic behind dimming arrays


Alterego
 Share

Recommended Posts

I've always avoided directly declaring arrays by instead using StringSplit because I could never get them to work properly; they always cause an error.

The help file says I can declare an array in the following ways:

; Example 2 - Declaring arrays
Dim $weeklyWorkSchedule[$daysWorking]
Global $chessBoard[8][8]
Local $mouseCoordinates[2], $windowStats[4]

Here is an example segment of code, where count turns out to be 9 in this case.

For $process = 1 to $count
        $hyperlink[$process] = StringInStr($pageText,$pattern1)
        MsgBox(1,"",$hyperlink[$process])
    Next
If I don't declare $hyperlink at all it gives "You must DIM an array before you can assign to it." So, I decide to DIM it in one of two methods at the beginning of my program:
Global $hyperlink
Global $process
Global $hyperlink[$process]

or 

Global $hyperlink[0][0][0][0][0][0][0][0][0][0][0]
The former returns
==> Array variable subscript badly formatted.: 
Global $hyperlink[$process] 
Global $hyperlink[^ ERROR
and the latter returns
Array variable subscript badly formatted.: 
Global $hyperlink[0][0][0][0][0][0][0][0][0][0][0] 
Global $hyperlink[^ ERROR

Can anyone help me out?

Edited by Alterego
Link to comment
Share on other sites

  • Developers

With DIM you basically define the array dimension...

Global $hyperlink[9] would mean you can use $hyperlink[0] till $hyperlink[8].

this gives an error since $process = 0 when the array is created..

Global $hyperlink
Global $process
Global $hyperlink[$process]
Edited by JdeB

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

Well what's the point of declaring an array with 0 indexes?

Dim $arrayName[0] = "Hey I'm gonna make an array, but I can't put anything in it"

See what I mean?

What you're probably experiencing problems is that the arrays start at 0 when you predeclare them.

So if you go like this:

Dim $array[3]

Then the valid variables there are $array[0], $array[1], and $array[2]. See, there are 3 items, but $array[3] is never actually reached.

So what you need is something like this:

Dim $hyperlink[$count]
For $process = 0 to $count - 1
        $hyperlink[$process] = StringInStr($pageText,$pattern1)
        MsgBox(1,"",$hyperlink[$process])
Next

OR this:

Dim $hyperlink[$count + 1]
For $process = 1 to $count
        $hyperlink[$process] = StringInStr($pageText,$pattern1)
        MsgBox(1,"",$hyperlink[$process])
Next

Try those out.

*Edit: Damn you JdeB, type slower. Now I just look like a rambling fool. :lmao:

Edited by Saunders
Link to comment
Share on other sites

That's for two dimensional arrays (personally I'm not a fan of how they work, but that's another story). I guess it's like a way of more easily organizing information in array without having to use two seperate arrays.

You'd do something like this

Dim $myArray[3][5]

Then you'd have all these items in the array:

$myArray[0][0]

$myArray[0][1]

$myArray[0][2]

$myArray[0][3]

$myArray[0][4]

$myArray[1][0]

$myArray[1][1]

$myArray[1][2]

$myArray[1][3]

$myArray[1][4]

$myArray[2][0]

$myArray[2][1]

$myArray[2][2]

$myArray[2][3]

$myArray[2][4]

Link to comment
Share on other sites

That's for two dimensional arrays (personally I'm not a fan of how they work, but that's another story). I guess it's like a way of more easily organizing information in array without having to use two seperate arrays.

I use them to organize data in a two dimensional table with an X and Y component.

$array[3][3]

could be used as a base for a simple "naughts and crosses" game.

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

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...