Jump to content

Need help with syntax for embedded arrays


Recommended Posts

OK, so I discovered from reading this: Arrays in Arrays that you can easily create and read from embedded arrays. Cool! 

To read the data stored in the embedded array, you enclose the left-hand side of the expression inside parentheses, as shown in the example from that tutorial (modified by myself for a test):

Local $aContainerArray[2]
Local $aInternalArray_0[2] = ["Internal-0-0","Internal-0-1"]
Local $aInternalArray_1[2] = ["Internal-1-0","Internal-1-1"]
$aContainerArray[0] = $aInternalArray_0
$aContainerArray[1] = $aInternalArray_1

Local $this = ($aContainerArray[0])[1]
ConsoleWrite("1 element of InternalArray_0: " & $this & @CRLF)
ConsoleWrite("0 element of InternalArray_1: " & ($aContainerArray[1])[0] & @CRLF)
ConsoleWrite("0 element of InternalArray_0: " & ($aContainerArray[0])[1] & @CRLF)

Note  the "$this" assignment syntax with the parentheses. Now, all of that works fine, but what's the syntax for storing a new value into the embedded array?  It seems to me it should be:

($aContainerArray[0])[1] = "Animal"

Because it's the mirror image of the $this assignment. But it produces a syntax error! "error: Statement cannot be just an expression."

So what's the correct syntax for storing data into an embedded array?

 

Link to comment
Share on other sites

  • Moderators

Mbee,

I do not believe that you can store data in an embedded array, just read it. To change the content of an embedded array, you need to extract it, amend the content and then reinsert it in the outer array.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

My great thanks to you, @Melba23 !  You've saved me hours of head-bashing!

I thought there were no Indirect operators native to AutoIt (though I imagine there are custom approaches, such as using DLLStructs or handles/pointers), but there one was with this parentheses business.  But as you point out, it's one-way (read) only.  I'm sure what you suggested as an alternative will work; it's just so... well, maybe not "ugly", but surely "inelegant".

Thanks for the super-fast response!

Link to comment
Share on other sites

a possible workaround....(?):

#include <array.au3>
Local $aContainerArray[2]
Local $aInternalArray_0[2] = ["Internal-0-0", "Internal-0-1"]
Local $aInternalArray_1[2] = ["Internal-1-0", "Internal-1-1"]
$aContainerArray[0] = $aInternalArray_0
$aContainerArray[1] = $aInternalArray_1

Local $this = ($aContainerArray[0])[1]
ConsoleWrite("1 element of InternalArray_0: " & $this & @CRLF)
ConsoleWrite("0 element of InternalArray_1: " & ($aContainerArray[1])[0] & @CRLF)
ConsoleWrite("0 element of InternalArray_0: " & ($aContainerArray[0])[1] & @CRLF)

_ArrayDisplay($aContainerArray[0], 'Before')

_SubArraySet($aContainerArray[0], 0, 'Animal')

_ArrayDisplay($aContainerArray[0], 'After')

Func _SubArraySet(ByRef $subarray, $iElement, $value)
    $subarray[$iElement]  = $value
EndFunc   ;==>Test

 

P.S,

Since $subarray is passe ByRef it shouldn't be extracted, but accessed directly in the $aContainerArray...?? .. or not?

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

  • Moderators

Chimp,

Interesting - I did not think that would work - but it obviously does. Good on you for finding out that it does.

I think that the sub-array is indeed extracted "under the hood" - I seem to remember from a previous discussion that if arrays passed ByRef are modified then they are copied, altered and rewritten, which would explain why it works.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

2 hours ago, Melba23 said:

I seem to remember from a previous discussion that if arrays passed ByRef are modified then they are copied, altered and rewritten

Sorry, but the only argument to pass ByVal is to save the time and after reading:

Quote

The ByRef keyword indicates that the parameter should be treated as a reference to the original. By default the parameter is copied into a new variable but ByRef links the new variable to the original. Note that not only a named variable can be passed for a ByRef parameter - unnamed temporary variables, such as function return values, may be passed as ByRef parameters as well. However, a literal cannot be passed to a ByRef parameter. ByRef should be used when passing large amounts of data (such as the contents of a file) where copying all the data would impose a significant performance penalty. Another advantage is that passing a parameter ByRef when the function is intended to change the content of the parameter removes any requirement to Return the changed value as the original is directly affected.

i hope AutoIt does this in same way as other programming languages.

Edited by AutoBert
Link to comment
Share on other sites

  • Moderators

AutoBert,

Why are you sorry?

Previous discussions with the Devs have explained that if an array is not passed ByRef, AutoIt will not make a copy unless the array content is altered within that function.  As I stated above, I also seem to recall from another discussion that even if an array is passed ByRef then it will be actually copied, modified and rewritten if altered. To me that would make perfect sense as the internal core code would be very similar in the 2 cases. And as long as the ByRef case is transparent to the user, why should we care how it is dealt with "under the hood"?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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