Jump to content

Understanding Arrays


Recommended Posts

Ok i have a problem.

I am currently reading the help file and looking over code trying to understand and learn autoit but i am stuck on arrays.

i see the example in the help file which is

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

I dont seem to understand how they work, i know here it says Declaring which has to do with variables but i just dont know how they work.

Any kind of help and understanding will be a big help.

Thanks in advance

[font="Courier"]Dont do things bass ackwards?[/font]
Link to comment
Share on other sites

Array 101

for a simple array, we will make one with 3 elements, give it some values and then display them with a for statement.

dim $array[3]; this made $array[0]  $array[1] and $array[2]
$array[0]=2
$array[1]="bob"
$array[2]="sam"
for $i=0 to 2
msgbox(1,"display $array[" & $i & "]",  $array[$i] ,5)
next

Same thing but with Stringsplit

$array=Stringsplit("bob,sam",","); this makes an array with comma delimiters.
for $i=0 to 2
msgbox(1,"display $array[" & $i & "]",  $array[$i] ,5)
next

you can also dimention arrays with variables.

$x=3
dim $array[$x]; this made $array[0]  $array[1] and $array[2]
$array[0]=2
$array[1]="bob"
$array[2]="sam"
for $i=0 to 2
msgbox(1,"display $array[" & $i & "]",  $array[$i] ,5)
next

They get complex if you need them too.

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

These have to do with global values vs local values.

When using functions, you can have variables that only are in the function(LOCAL), or make them GLOBAL to the whole script.

Before you can add values to the elements of an array, you need to DIMention it (unless you use StringSplit which dimentions it at creation and assisgns the first element as the quantity of elements besides itself.)

Dim $array[3]

Global $array[3]

Local $array[3]

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

What is happening in each of those statements is that chunks of memory are being set aside for the purpose of a set of variables, each one accessable using the array's name and the element's index number.

Dim $MyVar

This sets aside room for 1 variable, giving it the name MyVar. The variable is accessable by using the variable's name. i.e. $MyVar.

Dim $MyArray[12]

This sets aside room for 12 variables in an array, called MyArray. They are accessable using the array's name and the element's index number, which in this case could be any number from 0 through 11. Why throw away a perfectly good number like 0? :whistle: They can be used very similarily after that; just do not assign a simple value to the array or you wipe it out.

$MyVar = "Test"

$MyArray[3] = "Test 2"

MsgBox(0, "Title", "$MyVar = " & $MyVar)

MsgBox(0, "Title", "$MyArray[3] = " & $MyArray[3])

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

Nice explanation.

Here is something wierd to look at :whistle:

Opt("MustDeclareVars", 0)      ;0=no, 1=require pre-declare
dim $array[2]
$array[0]=" there"
$array[1]=" Fred."
MsgBox(1,"Test1",$array&$array[0]&$array[1])
MsgBox(1,"Test2",$array[0]&$array[1])

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

ok so its like

Dim $count[5]

$count[0] = 1

$count[1] = 2

$count[2] = 3

$count[3] = 4

$count[4] = 5

??

So arrays are like a variable with sorta like a drop down menu :whistle:

but this seems to have me stuck

$chessBoard[8][8] how does that double thing work?

[font="Courier"]Dont do things bass ackwards?[/font]
Link to comment
Share on other sites

A chessboard is an 8x8 grid. You could map out each chess piece with this array.

Been a while since I played chess but.

Dim $chessBoard[8][8]

$chessBoard[0][0]="rook"

$chessBoard[0][1]="bishop"

$chessBoard[0][2]="knight"

$chessBoard[0][3]="queen"

$chessBoard[0][4]="King"

$chessBoard[0][5]="knight"

$chessBoard[0][6]="bishop"

$chessBoard[0][7]="rook"

$chessBoard[1][0]="pawn"

$chessBoard[1][1]="pawn"

$chessBoard[1][2]="pawn"

$chessBoard[1][3]="pawn"

$chessBoard[1][4]="pawn"

$chessBoard[1][5]="pawn"

$chessBoard[1][6]="pawn"

$chessBoard[1][7]="pawn"

$chessBoard[2][0]=

$chessBoard[2][1]=

$chessBoard[2][2]=

$chessBoard[2][3]=

.....

$chessBoard[7][6]="bishop"

$chessBoard[7][7]="rook"

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

  • Administrators

ok so its like

Dim $count[5]

$count[0] = 1

$count[1] = 2

$count[2] = 3

$count[3] = 4

$count[4] = 5

??

So arrays are like a variable with sorta like a drop down menu  :whistle:

but this seems to  have me stuck

$chessBoard[8][8] how does that double thing work?

Ever seen post trays or a big rack of mailboxes/pigeon holes that look like a big grid? That's pretty much an array. Each "hole" can contain something and is referenced by a name like John Doe or something. In computing terms we just use numbers instead.
Link to comment
Share on other sites

$Chess[5][5]

$Chess[0][0] = 11
$Chess[0][1] = 12
$Chess[0][2] = 13
$Chess[0][3] = 14
$Chess[0][4] = 15
$Chess[1][0] = 16
$Chess[1][1] = 17
$Chess[1][2] = 18
$Chess[1][3] = 19
$Chess[1][4] = 100
$Chess[2][0] = 111
$Chess[2][1] = 112
$Chess[2][2] = 113
$Chess[2][3] = 114
$Chess[2][4] = 115
$Chess[3][0] = 116
$Chess[3][1] = 117
$Chess[3][2] = 118
$Chess[3][3] = 119
$Chess[3][4] = 120
$Chess[4][0] = 121
$Chess[4][1] = 122
$Chess[4][2] = 123
$Chess[4][3] = 124
$Chess[4][4] = 125

Is that correct?

And the basic is

$Board[8]

$Board[0] = 2
$Board[1] = 3
$Board[2] = 4
$Board[3] = 5
$Board[4] = 6
$Board[5] = 7
$Board[6] = 8
$Board[7] = 9
[font="Courier"]Dont do things bass ackwards?[/font]
Link to comment
Share on other sites

I like the stalls analagy.

If you were to write down names in a notebook:

Thinking about an analagy for 3.

1) $names[$how_many_lines_on_a_page]

2) $names[$how_many_lines_on_a_page][$how_many_pages_in_a_book]

3) $names[$how_many_lines_on_a_page][$how_many_pages_in_a_book][$how_many _books]

dim $names[$linenum][$pagenum][$booknum]

more useless array work:

store every pixel into an array
$begin = TimerStart()
Dim $pixel[@DesktopWidth][@DesktopHeight]

For $x=0 To @DesktopWidth-1
    For $y= 0 To @DesktopHeight-1
  $pixel[$x][$y]=PixelGetColor ($x,$y) 
    Next
Next

$dif = TimerStop($begin)
$pixel=0; release the memory
$pixels=@DesktopWidth*@DesktopHeight
MsgBox(1,"Time for "&$pixels&" pixels","Every pixel put into an array in "&int($dif/1000)&"seconds")

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

It is now your obligation to answer this question, the next time it arrives.... Congratulations... 

Thank you all for the help i appreciate it B)

I will toy around with this for a while befor i move on again thanks :whistle:

[font="Courier"]Dont do things bass ackwards?[/font]
Link to comment
Share on other sites

Here is something weird to look at :whistle:

Opt("MustDeclareVars", 0)    ;0=no, 1=require pre-declare
dim $array[2]
$array[0]=" there"
$array[1]=" Fred."
MsgBox(1,"Test1",$array&$array[0]&$array[1])
MsgBox(1,"Test2",$array[0]&$array[1])
The problem here is referring to an array ($array) as a normal string variable, which is what is contained in $array[0] and $array[0]. This invalid reference seems to have caused to expression evaluator to bomb out, returning an empty string. Edited by Nutster

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

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