# Stuck on Arrays again

## Recommended Posts

I'm needing to get some data into and out of a two dimensional Array, and I just cant get my head around how.

eg

I have this string "12,34,56,78,90,23,45,67,89,01,23,45,67,89,00"

I have no problem turning that into a 15 element array using

```\$snum = "12,34,56,78,90,23,45,67,89,01,23,45,67,89,00,"
\$anum = _StringExplode(\$snum,",",0)```

But I need to realize how to add a second dimension to that Array, or even create if differently

My goal being, that when a \$variable matches an element in the \$anum array, that \$variable becomes the constant in a second dimension corresponding to that element.

I've had a look around the helpfile and forum, but not really understanding what I am reading,

This is a little embarrassing, but I dont even understand what this means

To initialize an array, specify the values for each element inside square brackets, separated by commas. For multiple dimensions, nest the initializers.

All and any advice greatly appreciated. Edited by JohnOne

Monkey's are, like, natures humans.

##### Share on other sites

Initializing a 2D array:

```#include <Array.au3>

Global \$aTest[3][3] = [["0 - 0", "0 - 1" , "0 - 2"],["", "1 - 1"],["2 - 0", "2 - 1", "2 - 2"]]
_ArrayDisplay(\$aTest, "2D Array")```

Creating a 2D array from a 1D:

```#include <Array.au3>

Global \$sString = "1,2,3,4,5"
Global \$aTest1 = StringSplit(\$sString, ",")
Global \$aTest2[Ubound(\$aTest1)][2]

_ArrayDisplay(\$aTest1, "1D Array")

\$aTest2[0][0] = \$aTest1[0]
For \$n = 1 To Ubound(\$aTest2) - 1
\$aTest2[\$n][0] = \$aTest1[\$n]
\$aTest2[\$n][1] = Asc(\$aTest1[\$n]) ; ASCII code
Next

_ArrayDisplay(\$aTest2, "2D Array")```

There is a tutorial on arrays somewhere in the AutoIt Wiki.

Edit: Missing " = " in first demo script. Missing #include and closing quote on second.

Let that be a lesson to me - don't code demos in the forum editor...

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

##### Share on other sites

Initialize an array:

- one dimension with 10 elements

`Local \$array1D[10]`

- two dimensions with 10 elemnts in 1st an 2 elements in 2nd dimension

`Local \$array2D[10][2]`

I hope, now you understand it.

Best Regards BugFix

##### Share on other sites

PsaltyDS, Those examples are spitting out syntax errors

I dont know enough to fix them

EDIT: just a couple of missing ""

I tried this to populate it, but it just displays an empty array

```#include <Array.au3>
#include <String.au3>

Local \$acds[9][2]

\$scds0 = "2,2,3,3,3,4,4,4,4"
\$scds1 = "A,A,B,B,B,C,C,C,C"
\$acds[0][0] = _StringExplode(\$scds0, ",", 0)
\$acds[0][1] = _StringExplode(\$scds1, ",", 0)
_ArrayDisplay(\$acds)
\$icount = UBound(\$acds) - 1
MsgBox(0, "", \$icount)```
Edited by JohnOne

Monkey's are, like, natures humans.

##### Share on other sites

Ok, thanks gents, I think I have it now.

Monkey's are, like, natures humans.

##### Share on other sites

PsaltyDS, Those examples are spitting out syntax errors

My bad. I should know better by now than to code a demo in the forum edit box. I have tweaked the examples I posted and they work correctly now. Hope they help shed light on the topic.

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

##### Share on other sites

I tried this to populate it, but it just displays an empty array

```#include <Array.au3>
#include <String.au3>

Local \$acds[9][2]

\$scds0 = "2,2,3,3,3,4,4,4,4"
\$scds1 = "A,A,B,B,B,C,C,C,C"
\$acds[0][0] = _StringExplode(\$scds0, ",", 0)
\$acds[0][1] = _StringExplode(\$scds1, ",", 0)
_ArrayDisplay(\$acds)
\$icount = UBound(\$acds) - 1
MsgBox(0, "", \$icount)```
Try that this way:
```#include <Array.au3>
#include <String.au3>

Global \$vcds0, \$vcds1, \$acds2D[1][1]

\$vcds0 = "2,2,3,3,3,4,4,4,4" ; A string
\$vcds1 = "A,A,B,B,B,C,C,C,C" ; another string
\$vcds0 = _StringExplode(\$vcds0, ",", 0) ; Now it's a 1D array
\$vcds1 = _StringExplode(\$vcds1, ",", 0) ; another 1D array
If (IsArray(\$vcds0) = 0) Or (UBound(\$vcds0) <> UBound(\$vcds1)) Then Exit; Error, they must be arrays of the same size
ReDim \$acds2D[UBound(\$vcds0)][2] ; Re-declared as a 2D array of correct size

; Move the data from the 1D arrays into the 2D array
For \$n = 0 To UBound(\$vcds0) - 1
\$acds2D[\$n][0] = \$vcds0[\$n]
\$acds2D[\$n][1] = \$vcds1[\$n]
Next

_ArrayDisplay(\$acds2D, "2D Array")```

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

##### Share on other sites

Excellent bud, cheers,

Quick question though

Could I create the first two 1D arrays locally, to create the global 2D array, and then set the others to =0 to save memory ?

Monkey's are, like, natures humans.

##### Share on other sites

Quick question though

Could I create the first two 1D arrays locally, to create the global 2D array, and then set the others to =0 to save memory ?

Yes. "Locally" implies inside a function though, and all the local variables from inside a function get release when you exit the function anyway (unless you use the new 'Static' type in the Beta). So it won't hurt, but may not be necessary.

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

## Create an account

Register a new account