Jump to content

Arrays within an array


Recommended Posts

When storing an array within an array, is it possible to directly access the "internal" array's contents?

For example:

dim $a[5]
for $i = 0 to 4
  $a[$i] = _usefullfunc($i)
next

msgbox(0,"test", $a[2][1][1]) ;; reading the value this way "sometimes" works
$a[2][1][1] += 1              ;; writing the value this way does not seem to work at all


func _usefullfunc($param)
  local $b[2][2]
  ;; do some things to populate a two dimensional array with useful data
  return $b
endfunc

At present, I have to pull the "internal" array out into a temporary variable, and work from there. This works, but makes for some rather messy code.

Link to comment
Share on other sites

  • Moderators

willichan,

As far as I know, you have to extract the "inner" array from the "outer" array before you can use it. This is because Autoit does not know that you have an array within the "outer" element and so does not know how to address the "inner" elements directly.

By the way, have you read the Help file comments about putting arrays into arrays? Although not forbidden, there are some issues. :

Anyway, if I had to address "inner" array elements often within a script, I would consider a single multiple dimension array which would allow direct addressing of individual elements. I will see if I can come up with an example later. :blink:

M23

Edit: That was easier than I thought: :nuke:

; These are the inner arrays you want to store
Global $aInner_Array_1[2][4] = [["A", "B", "C", "D"], ["W", "X", "Y", "Z"]]
Global $aInner_Array_2[4] = [0, 1, 2, 3]

; So declare an outer array with the first dimension set to the number of inner arrays you want to store
; and the other dimensions big enough to hold the biggest of the inner arrays in EACH dimension
Global $aOuter_Array[2][4][4]

; And then move the inner arrays inside - Array name, Index ID for inner array, number of dimensions of inner array
_Write_Array($aInner_Array_1, 0, 2)
_Write_Array($aInner_Array_2, 1, 1)

; And you can easily access the individual elements like this - Index ID for inner array, indices of element to read
; This should give us "Y"
ConsoleWrite(_Read_Array(0, 1, 2) & @CRLF)
; This should give us "1"
ConsoleWrite(_Read_Array(1, 1) & @CRLF)

Func _Write_Array($aArray, $iIndex, $iDimensions = 1)

    Switch $iDimensions
        Case 1
            For $i = 0 To UBound($aArray) - 1
                $aOuter_Array[$iIndex][$i][0] = $aArray[$i]
            Next
        Case 2
            For $i = 0 To UBound($aArray) - 1
                For $j = 0 To UBound($aArray, 2) - 1
                    $aOuter_Array[$iIndex][$i][$j] = $aArray[$i][$j]
                Next
            Next
    EndSwitch

EndFunc

Func _Read_Array($iIndex, $iDim_1, $iDim_2 = 0)

    Return $aOuter_Array[$iIndex][$iDim_1][$iDim_2]

EndFunc

I would add a lot of errorchecking code before using something like that for real :P . But I think the principle is clear.

Please ask if you have any questions. ;)

M23

Edited by Melba23

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

Nesting arrays don't work well in AutoIt or better, impose a lot of copying of inner arrays.

I see it as the indexing "operator" being bound to a previously parsed variable name, not to the "current object". If it was, the following would work:

Local $a[5] = [ 1, 2, 3, 4, 5]
Local $b[5] = [ 11, 12, $a, 14, 15]
ConsoleWrite(($b[2])[3] & @LF)

I'm not in the position to comment how exactly that translates in the guts of the interpretor.

Anyway, there doesn't seem to exist a reasonable workaround, so the best solution is to avoid such construct at any rate. A partial workaround might be to use DllStructs which may nest. But accessing elements comes with a cost in both overhead and code.

When I face a situation where such constructs come to the mind (heritage from other languages), I don't spend any time trying games to gain 1Mb of temp storage and I switch to a more steam-roller style solution: N-dim arrays or SQLite temp (memory) table or N-dim dictionary or anything else.

Of course, my SQLite-perverse nurture pushes me to favor a temp table, which can be saved or reloaded with a single function call and offers huge power to insert/query/update things.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

@Melba23, @jchd,

Thank you both. Although I normally would avoid doing this (based, yes, on the warning in the help), I needed it for this implementation. I often store data in arrays passed with ByRef to simulate object-like behavior (and store states and values in a single object-like variable). I have never needed to pass multiple arrays before, so this time around, it posed a nice challenge for me. Last night, I came up with the following solution (not too dissimilar to Melba23's) to do it.

Func _GetSubArray(ByRef $MainArray, $sub1, $sub2=-1, $sub3=-1)
  Local $temp
  $temp = $MainArray[$sub1]
  Switch @NumParams
    Case 2
      Return $temp
    Case 3
      Return $temp[$sub2]
    Case 4
      Return $temp[$sub2][$sub3]
  EndSwitch
EndFunc

Func _SetSubArray(ByRef $MainArray, $value, $sub1, $sub2=-1, $sub3=-1)
  Local $temp
  $temp = $MainArray[$sub1]
  Switch @NumParams
    Case 3
      $temp = $value
    Case 4
      $temp[$sub2] = $value
    Case 5
      $temp[$sub2][$sub3] = $value
  EndSwitch
  $MainArray[$sub1] = $temp
EndFunc

It appears to be working, but gives me headaches when something as simple as

$a[1][2][3] += 1

has to be written as

_SetSubArray($a, _GetSubArray($a, 1, 2, 3) + 1, 1, 2 ,3)
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...