Jump to content
Sign in to follow this  
faldo

Arrays

Recommended Posts

faldo

Hi,

I've been looking at the helpfiles in A3 for arrays, but i'm affraid it's not that well explaind.

I've heard that useing arrays in variables are very usefull to "clean up" the script.

If someone with knowlege could take a few minutes and create an example or two of a code without an array and then the same code with an array and explain what he did. 2 examples would be great, one very simple and one more dificult so i could compare them :idiot:

Thanx in advance.

Share this post


Link to post
Share on other sites
SlimShady

Is this helpful?

;WITHOUT an array
Dim $var0, $var1, $var2, $var3
$var0 = "value0"
$var1 = "value1"
$var2 = "value3"
$var3 = "value4"

;WITH an array (version 1)
Dim $var[4]
$var[0] = "value0"
$var[1] = "value1"
$var[2] = "value2"
$var[3] = "value3"

;WITH an array (version 2)
Dim $var[4]
For $i = 0 To 3
   $var[$i] = "value" & $i
Next

Share this post


Link to post
Share on other sites
phillip123adams

Hi,

I've been looking at the helpfiles in A3 for arrays, but i'm affraid it's not that well explaind.

I've heard that useing arrays in variables are very usefull to "clean up" the script.

If someone with knowlege could take a few minutes and create an example or two of a code without an array and then the same code with an array and explain what he did. 2 examples would be great, one very simple and one more dificult so i could compare them :idiot:

Thanx in advance.

<{POST_SNAPBACK}>

Here's some examples. The first is the non-array method in which every item has a variable name.

The 2nd and 3rd examples use a one dimension array to do the same task as the non-array method (only one variable name).

The 4th example uses a 2 dimension array.

; Example 1 (non-array method)
MsgBox(4096, "Non-Array", "Press OK for non-array method.")
; Assign a variable for each item.
$sToy1 = "Doll"
$sToy2 = "Drum"
$sToy3 = "Bike"
$sToy4 = "Train"
$sToy5 = "Blocks"
; Display each item
MsgBox(4096, "No Array", "Toy 1: " & $sToy1)
MsgBox(4096, "No Array", "Toy 2: " & $sToy2)
MsgBox(4096, "No Array", "Toy 3: " & $sToy3)
MsgBox(4096, "No Array", "Toy 4: " & $sToy4)
MsgBox(4096, "No Array", "Toy 5: " & $sToy5)


; Example 2 (array method 1)
MsgBox(4096, "Array 1", "Press OK for 1st array method.")
; An array must be dimensioned before use.
Dim $asMyToys1[5]
; One way to put items into an array.
; Arrays begin with an index value of
; zero so this example is 0 through 4.
$asMyToys1[0] = "Doll"
$asMyToys1[1] = "Drum"
$asMyToys1[2] = "Bike"
$asMyToys1[3] = "Train"
$asMyToys1[4] = "Blocks"
; Display each item in the array one by one (starting with index zero)
For $i = 0 to UBound($asMyToys1) - 1
   Msgbox(4096, "Array 1", "Toy " & $i + 1 & ": " & $asMyToys1[$i])
Next


; Example 3 (array method 2)
MsgBox(4096, "Array 2", "Press OK for 2nd array method.")
; Another way to put items into an array (cleaner and simpler).
$sMyToys2 = "Doll,Drum,Bike,Train,Blocks"
; Create the array.  Arrays created with StringSplit use
; array index zero to store the number of items that wer split.
; Since this example has 5 items, index zero will be 5,
; and the array size will be 6.  You do not have to 
; dimension the array as StringSplit does that for you.
$asMyToys2 = StringSplit($sMyToys2, ",")
; Display each item in the array one by one (starting with index 1)
For $i = 1 to $asMyToys2[0]
   Msgbox(4096, "Array 2", "Toy " & $i & ": " & $asMyToys2[$i])
Next


; Example 4 (a two dimension array)
MsgBox(4096, "Two Dimension Array", "Press OK for a Two Dimension Array.")
; Dimension the array (over sized on purpose)
Dim $asEveryBodyToys[3][20]
; Put the items for person 1 (Mary) into the array
$aToys = StringSplit("Mary,Doll,Tea Set,Jacks,Radio,Puzzles", ",")
$asEveryBodyToys[0][0] = $aToys[1]
For $i = 1 to $aToys[0]
   $asEveryBodyToys[0][$i - 1] = $aToys[$i]
Next
; Put the items for person 2 (Joe) into the array
$aToys  = StringSplit("Joe,Drum,Bike,Train,Blocks,Cars", ",")
$asEveryBodyToys[1][0] = $aToys[1]
For $i = 1 to $aToys[0]
   $asEveryBodyToys[1][$i - 1] = $aToys[$i]
Next
; Display each item in the array for each person (index 2 is empty)
For $i = 0 to UBound($asEveryBodyToys) - 1
   MsgBox(4096, $asEveryBodyToys[$i][0], $asEveryBodyToys[$i][1] & @lf & $asEveryBodyToys[$i][2] & @lf & $asEveryBodyToys[$i][3] & @lf & $asEveryBodyToys[$i][4] & @lf & $asEveryBodyToys[$i][5])
Next

; An array can be up to 64 dimensions, but I can't think that
; hard right now, and I doubt I could think past 3 dimensions anyway.
Exit

Phillip

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.