Jump to content

Assign a value to an array in array element


j0kky
 Share

Recommended Posts

Hi guys,

talking about array in array, I would like to directly assign a value to an element in an array contained in another array.

While it is quite simple to access the value if it is already setted:

Local $a[3] = [1, 2], $b[2] = [3, 4]
$a[2] = $b
ConsoleWrite(($a[2])[0] & " " & ($a[2])[1] & @CRLF)

If I try to write directly to the element, I get an error:

Local $a[3] = [1, 2]
Local $b[2]
$a[2] = $b
($a[2])[0] = 1

Is assigning first every value of the contained array and then including it in the container the only way?

Edited by j0kky
Link to comment
Share on other sites

You need a helper function with ByRef parameter.

 

Simple example:

#include <Array.au3>
Local $a[3] = [1, 2], $b[2] = [3, 4]
$a[2] = $b

ConsoleWrite(($a[2])[0] & " " & ($a[2])[1] & @CRLF)

ModArray($a[2], 0, "Test")

_ArrayDisplay($a[2])

Func ModArray(ByRef $aArray, $iElement, $value)
    $aArray[$iElement] = $value
EndFunc

Otherwise a copy of the array will be created.

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Thank you for support!

Writing a function with a ByRef param is a smart solution, anyhow it could be useful to directly assign the value to the element.

I'll open a feature request in the bug tracker, maybe in some of the next Autoit version it will be implemented.

Link to comment
Share on other sites

3 hours ago, j0kky said:

I'll open a feature request in the bug tracker, maybe in some of the next Autoit version it will be implemented.

Soon is christmas, I would suggest to send the feature list rather to Santa Claus. 

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

On ‎19‎.‎11‎.‎2016‎. at 3:29 PM, j0kky said:

Hi guys,

talking about array in array, I would like to directly assign a value to an element in an array contained in another array.

While it is quite simple to access the value if it is already setted:

Local $a[3] = [1, 2], $b[2] = [3, 4]
$a[2] = $b
ConsoleWrite(($a[2])[0] & " " & ($a[2])[1] & @CRLF)

If I try to write directly to the element, I get an error:

Local $a[3] = [1, 2]
Local $b[2]
$a[2] = $b
($a[2])[0] = 1

Is assigning first every value of the contained array and then including it in the container the only way?

What error? If you see error, then it's not by AutoIt because your code is perfectly valid. It's only that the lase line is not an assignment.

There is no error.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

5 minutes ago, trancexx said:

It's only that the lase line is not an assignment.

($a[2])[0] = 1

The problem is exatly that this line is not considered as an assignment, but ($a[2])[0] is correctly parsed when you try to read from it.

Edited by j0kky
Link to comment
Share on other sites

1 hour ago, trancexx said:

In both cases you read from it. That's the whole thing.

Yes, now it is clear, but it sounds strange to me because if you declare a simple array and try to do the same thing, Autoit doesn't read from it but assigns to it the value.

Local $a[2]
$a[0] = 1

 

Edited by j0kky
Link to comment
Share on other sites

28 minutes ago, j0kky said:

Yes, now it is clear, but it sounds strange to me because if you declare a simple array and try to do the same thing, Autoit doesn't read from it but assigns to it the value.

Local $a[2]
$a[0] = 1

 

That's because you're showing different case.

What you did earlier was more like this:

Local $a[2]
($a[0]) = 1

What does this do?

 

 

 

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

No problem @trancexx, I'm here to learn :)

I'll try to pose the issue in another way (but I think you already get my point):

If I'm working with a simple array:

Local $a[2]
$a[0] = 1
ConsoleWrite(($a)[0] & @CRLF)
ConsoleWrite($a[0] & @CRLF)

ConsoleWrite outputs are identical.

But if I'm dealing with an array into an array:

Local $a[3] = [1, 2], $b[2] = [3, 4]
$a[2] = $b
ConsoleWrite(($a[2])[0] & @CRLF)
ConsoleWrite($a[2][0] & @CRLF)

The second one (obviously) returns an error because it searches for the 2nd dimension of $a.

 

Edited by j0kky
Link to comment
Share on other sites

  • 4 years later...

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

×
×
  • Create New...