Jump to content

setting array values


Recommended Posts

Ok, I am a noob here but I have tried this alot of ways and can't get it to work.

I would like to set array values by passing an array. Like in C++ (iirc) you can write

Global Myarray[2][2]

Myarray = {{1,2},{3,4}}

and in autoit

Global $Myarray[2][2] = [[1,2],[3,4]]

but when I try setting the variable after declaration

$Myarray = {{1,2},{3,4}} or $Myarray = [[1,2],[3,4]] or

$Myarray = $Prevdeclarearray

I get errors compiling

Can you pass an array into an array in this way without re-declaring the array? Sorry it is so noobish but I did try to get the answer myself (fail) and thanks for the help.

Link to comment
Share on other sites

This copies the entire array:

$avDestination = $avSource

If you want to copy a subset of the elements, it has to be done one element at a time with a For/Next loop or something similar.

:blink:

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
Link to comment
Share on other sites

Ok thanks for the help.

But here is what happens when i try this:

Global $pw1[2] = [1,0]

;at this point everything is OK - $pw1 = [1,0]

$pw1 = [0,1]

Now i get an error like this:

C:\Program Files (x86)\AutoIt3\SciTE\myscript.au3(15,8) : ERROR: syntax error

$pw1 = [

~~~~~~~^

I get similar erorw when trying to write an array with an array

$destination = $source

so what noob thing am i doing wrong????

Link to comment
Share on other sites

Ok thanks for the help.

But here is what happens when i try this:

Global $pw1[2] = [1,0]

;at this point everything is OK - $pw1 = [1,0]

$pw1 = [0,1]

Now i get an error like this:

C:\Program Files (x86)\AutoIt3\SciTE\myscript.au3(15,8) : ERROR: syntax error

$pw1 = [

~~~~~~~^

I get similar erorw when trying to write an array with an array

$destination = $source

so what noob thing am i doing wrong????

Ok, I got the $arraydest = $arraysource working

But I assume there is no way to do something like this without redeclaring an array

$myarray[2][2] = [[1,2],[3,4]]

???

Thanks again for all the help

Link to comment
Share on other sites

Ok, I got the $arraydest = $arraysource working

But I assume there is no way to do something like this without redeclaring an array

$myarray[2][2] = [[1,2],[3,4]]

Well, no, you can't re-declare (REDIM) with initialization values. And while you could just do DIM/GLOBAL/LOCAL again, that breaks down if used with functions as ByRef.

But who cares? Nothing prevents you from declaring a temp and copying it in at any time:

#Include <Array.au3>

Global $aData[2][2] = [["a", "b"], ["c", "d"]]
_ArrayDisplay($aData, "$aData")

_ODD($aData)
_ArrayDisplay($aData, "$aData")

_EVEN($aData)
_ArrayDisplay($aData, "$aData")


Func _ODD(ByRef $avArray)
    Local $avTemp[2][2] = [[1, 3],[5, 7]]
    $avArray = $avTemp
EndFunc

Func _EVEN(ByRef $avArray)
    Local $avTemp[2][2] = [[2, 4],[6, 8]]
    $avArray = $avTemp
EndFunc

:blink:

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
Link to comment
Share on other sites

Well, no, you can't re-declare (REDIM) with initialization values. And while you could just do DIM/GLOBAL/LOCAL again, that breaks down if used with functions as ByRef.

But who cares? Nothing prevents you from declaring a temp and copying it in at any time:

#Include <Array.au3>

Global $aData[2][2] = [["a", "b"], ["c", "d"]]
_ArrayDisplay($aData, "$aData")

_ODD($aData)
_ArrayDisplay($aData, "$aData")

_EVEN($aData)
_ArrayDisplay($aData, "$aData")


Func _ODD(ByRef $avArray)
    Local $avTemp[2][2] = [[1, 3],[5, 7]]
    $avArray = $avTemp
EndFunc

Func _EVEN(ByRef $avArray)
    Local $avTemp[2][2] = [[2, 4],[6, 8]]
    $avArray = $avTemp
EndFunc

:blink:

Thanks alot that really helped

One final question along the same lines

I have had problems using arrays in if statements

global $state[2]=[0,0]

if $state = [0,0] then

But it wont work this way I have to use

if $state[0] and $state[1] = 0 then

am I doing this right?

Link to comment
Share on other sites

You can't directly compare array variables. Since AutoIt is so loosely typed, and an array can contain different variant types (not recommended, but often done anyway), it would require a thick manual just to define the comparison rules.

So in AutoIt you have iterate the compare yourself.

A quick _ArrayCompare() UDF with exactly the rules you want should be easy to write, though.

:blink:

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