Jump to content
Sign in to follow this  
Smiley357

multidimensional array

Recommended Posts

Smiley357

I have 2 questions about arrays.

1) Im trying to set some variable to an array. Here is a simple example that doesnt work. Can someone tell me what im doing wrong? Code 1 seems to have errors in how I declare the variable in the array. Code 2 works but this wont help me since I need to set each part of the array separately. Thanks!

Code 1

#include <Array.au3>
Local $avArray[2][1]

Local $avArray[0][0] = "0"
Local $avArray[0][1] = "1"
Local $avArray[1][0] = "2"
Local $avArray[1][1] = "3"

$var = $avArray[0][0]
MsgBox(0,'$var is: ', $var)
$var = StringLen($var)
MsgBox(0,'$var value is: ', $var)

Code 2

#include <Array.au3>
Local $avArray[2][3] = [[0,1,2],[3,4,5]]

$var = $avArray[0][0]
MsgBox(0,'$var is: ', $var)
$var = StringLen($var)
MsgBox(0,'$var value is: ', $var)

2) Im also trying to find the length of avArray[0][] and avVrray[1][] is there a better way of doing this?

Share this post


Link to post
Share on other sites
weaponx

Local $avArray[2][1]

should be

Local $avArray[2][2]

since you are using 0 and 1 in the second dimension

Share this post


Link to post
Share on other sites
Smiley357

Weaponx thx for the fast response but I still get a error.

ERROR: syntax error

Local $avArray[0][0] = "0"

~~~~~~~~~~~~~~~~~^

Share this post


Link to post
Share on other sites
JFee

get rid of the "Local" when you are assigning values to the array. Only use Local Global and Dim when you are declaring them


Regards,Josh

Share this post


Link to post
Share on other sites
martin

Weaponx thx for the fast response but I still get a error.

ERROR: syntax error

Local $avArray[0][0] = "0"

~~~~~~~~~~~~~~~~~^

It doesn't make sense to declare the array with zero elements. Weaponx already showed how to do it.

To initialize an array you need to do something like this

Local $avArray[2][2] = [["0",2][3,4]]

or if you just want to set one element then

Local $avArray[2][2]

$avArray[0][0] = "0"


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
Smiley357

get rid of the "Local" when you are assigning values to the array. Only use Local Global and Dim when you are declaring them

Thx. I didnt realize putting Local was also trying to declare a new array. I took the Local out and problem solved. Thx again.

It doesn't make sense to declare the array with zero elements. Weaponx already showed how to do it.

As you can probably tell im very new to AutoIt. But in most languages you have to declare the array size before you can use the array. So Local $avArray[2][2] would be standard.

To initialize an array you need to do something like this

Local $avArray[2][2] = [["0",2][3,4]]

This would be fine if I knew my variables. But as I stated this will not work for me.

Share this post


Link to post
Share on other sites
JFee

Martin was just talking about the same thing I was. When you were trying to set $avArray[0][0] = "0" it thought you were trying to declare a 0x0 array


Regards,Josh

Share this post


Link to post
Share on other sites
FreeFry

As you can probably tell im very new to AutoIt. But in most languages you have to declare the array size before you can use the array. So Local $avArray[2][2] would be standard.

It is standard in AutoIt too, but you only declare an Array once(and if you declare it outside a function in the Local spectrum, it will still be an Global array/variable).

This would be fine if I knew my variables. But as I stated this will not work for me.

If I understand you correctly, you can also initialize an array like this:

$Element1 = "element 1 value"
$Element2 = "element 2 value"
Dim $avArray[2][2] = [[$Element1, $Element2], [[$Element1, $Element2]]

Perhaps I'm off at this, but I dunno. muttley

Share this post


Link to post
Share on other sites
PsaltyDS

This would be fine if I knew my variables. But as I stated this will not work for me.

You can change the extent of an array without losing data using ReDim, provided you don't change the number of dimensions (indexes). For example:
#include <Array.au3>; for _ArrayDisplay() only

Global $avArray[2][2] = [[0,1], [2,3]]
_ArrayDisplay($avArray, "Before")

ReDim $avArray[3][4]
Global $iChar = Asc("A")
For $r = 0 To UBound($avArray) - 1
    For $c = 0 To UBound($avArray, 2) - 1
        If ($r <= 1) And ($c <= 1) Then 
            ContinueLoop
        Else
            $avArray[$r][$c] = Chr($iChar)
            $iChar += 1
        EndIf
    Next
Next
_ArrayDisplay($avArray, "After")

If that ReDim had changed the number of dimensions, for example from 2D to 3D - ReDim $avArray[2][2][2], then all previous data in the array would be lost.

muttley


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 this post


Link to post
Share on other sites
Smiley357

@FreeFry

You understand me correctly and that would work. Thanks for the suggestion.

@PsaltyDS

ReDim is very interesting. I will have to keep that in mind.

Thanks for all the help everyone.

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  

×