Jump to content

Recommended Posts

Posted

I have made some ok scripts with the help of this forum and lately have been seeing a lot of talk about arrays.

What are they and how are they useful?

Thanks in advance!

Posted

Well... If you use arrays, you dont have to use many different variables. Example:

Dim $array[5];Declares 5 arrays
$array[1] = "Hello!"
$array[2] = "Goodbye!"
$array[3] = "Some more arrays"
$array[4] = "Fourth array"

For $i = 1 to 4
$result = $array[$i] &@CRLF
Next

MsgBox(0, "", $result) 
;Result will be:
;Hello!
;Goodbye!
;Some more arrays
;Fourth array

And of course you can do a lot more things with arrays.

Posted

Arrays are a form of data structure.

If you need to keep track of a number of items tht are of the same data type, the best solution is to use an array. For example, a bank may want to keep track of the ballances for each of the 12 months, without arrays you could create 12 variables: (inefficent)

$Jan_balance
$Feb_balance
$Mar_balance
$Apr_balance
$May_balance
$Jun_balance
$Jul_balance
$Aug_balance
$Sep_balance
$Oct_balance
$Nov_balance
$Dec_balance

To use these variables, you must determine which month it is an then switch/ifelse among the correct variables.

If $month == 0 Then
   $jan_ballance += $amount_to_add
Elseif $month == 1 Then
   $feb_ballance += $amount_to_add
;... code cut ...
ElseIf $month == 11 Then
   $dec_ballance += $amount_to_add
Endif

Get the idea, but with arrays:

Dim $balance[12]
;; $balance[0] is january, $balance[11] is december
$balance[$month] += $amount_to_add

RECAP:

Declaration:

Dim $array_name[<number_of_elements>]

Access:

$array_name[<element_number>]

Remember that for Dim $array_name[12] makes a twelve element array, element numbers ranging from 0 to 11. (always start with 0!!!)

  • Moderators
Posted (edited)

Well... If you use arrays, you dont have to use many different variables. Example:

Dim $array[5];Declares 5 arrays
$array[1] = "Hello!"
$array[2] = "Goodbye!"
$array[3] = "Some more arrays"
$array[4] = "Fourth array"

For $i = 1 to 4
$result = $array[$i] &@CRLF
Next

MsgBox(0, "", $result) 
;Result will be:
;Hello!
;Goodbye!
;Some more arrays
;Fourth array

And of course you can do a lot more things with arrays.

Result this way will be: Fourth array

You need to have the $result in with the adding and Dim/Local the variable $result:

Dim $array[5];Declares 5 arrays
$array[1] = "Hello!"
$array[2] = "Goodbye!"
$array[3] = "Some more arrays"
$array[4] = "Fourth array"
Dim $result

For $i = 1 to 4
$result = $result & $array[$i] &@CRLF
Next

MsgBox(0, "", $result)
;Result will be:
;Hello!
;Goodbye!
;Some more arrays
;Fourth array

Edit

or with Beta: $result &= $array[$i] & @CRLF

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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
  • Recently Browsing   0 members

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