Jump to content
Sign in to follow this  
Chimp

question about array within array

Recommended Posts

Chimp

From the Autoit documentation, under the "variables" link I read:

It was said that an Array contains only one datatype of the same type. But technically speaking, a Variant in AutoIt can contain anything from a number to a boolean value. So an AutoIt-Array could also contain different types, even other Arrays:

$Array[0]=1
$Array[1]=true
$Array[2]="Text"
$Array[3]=$AnotherArray

 

Well, now if I have a simple 1D array, and one element of this contains another 1D array, how can I access the elements of the "nested" array?

 

Can I access $AnotherArray[2] directly from within $Array[3] in the following example?

Dim $AnotherArray[5] = ["zero", "one", "two", "three", "four"]
Dim $Array[4]

$Array[0] = 1
$Array[1] = True
$Array[2] = "Text"
$Array[3] = $AnotherArray

ConsoleWrite($Array[3][2]) ; <-- this is obviously wrong

; is there a way to read "directly" the element [2] of the nested array from $Array[3] ?

thanks


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

Share this post


Link to post
Share on other sites
Melba23

Pincopanco,

Not as far as I know - you have to extract it into a temporary variable first. Basically, how is Autoit to know that the outer array element is actually another array? ;)

M23

Edit: See below for a solution using the Beta - I forgot all about that. :>

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
0xdefea7

I do the same as Melba said, save to variable first, then access the array using the temporary variable. There is no way to do it directly.

Dim $AnotherArray[5] = ["zero", "one", "two", "three", "four"]
Dim $Array[4]
Dim $Temp

$Array[0] = 1
$Array[1] = True
$Array[2] = "Text"
$Array[3] = $AnotherArray

ConsoleWrite($Array[3][2]) ; <-- this is obviously wrong

$Temp = $Array[3] ;Grab the array and save to $Temp

ConsoleWrite($Temp[2] & @CRLF)  ;Prints 2 to the console

; is there a way to read "directly" the element [2] of the nested array from $Array[3] ?

Posted the code in case anyone else would like to follow along :)

Share this post


Link to post
Share on other sites
Chimp

Thanks Melba23 and 0xdefea7 for the quick response

I was hoping that it was possible :(

I tried with _ArrayDisplay() .... it shows a "hole" in that position.....  :thumbsdown:

(also _ArrayDisplay() left me alone :P )

Edited by Pincopanco

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

Share this post


Link to post
Share on other sites
abberration

This would kind of do what you want. I included an inputbox so you can see it can read the 2nd array from the first.

#include <array.au3>

Dim $AnotherArray[5] = ["zero", "one", "two", "three", "four"]
Dim $Array[4]

$input = InputBox("Enter number", "Enter number between 0 and 4")

$Array[0] = 1
$Array[1] = True
$Array[2] = "Text"
$Array[3] = $AnotherArray[$input]

_ArrayDisplay($Array)

RAID Calculator | Software Installer

The truth has been suppressed since the dawn of time.

Share this post


Link to post
Share on other sites
Chimp

Hi Abberration

thanks for the reply, for a moment I believed in a solution ...
but in this way the $Array[3] element contains only one value and not the whole $AnotherArray
This is not an Array within an Array ;)
thanks again for trying


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

Share this post


Link to post
Share on other sites
jchd

The beta offers a solution:

Local $a[2] = ["x", "y"]
Local $b[2] = [1, $a]
ConsoleWrite(($b[1])[0] & @LF)

Use it or wait for the release.


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)

Share this post


Link to post
Share on other sites
AdamUL

You can do this with the AutoIt Beta.

 

Local $AnotherArray[5] = ["zero", "one", "two", "three", "four"]
Local $Array[4]

$Array[0] = 1
$Array[1] = True
$Array[2] = "Text"
$Array[3] = $AnotherArray

;~ ConsoleWrite($Array[3][2]) ; <-- this is obviously wrong
ConsoleWrite(($Array[3])[2] & @CR) ;<-- Works with AutoIt Beta v3.3.9.18
 
I don't know if you want to use the Beta version though.

Adam

Edit: jchd beat me to it.

Edited by AdamUL

Share this post


Link to post
Share on other sites
Chimp

Thanks jchd & AdamUL

this is what I was looking for :)
I am using AutoIt 3.3.8.1
but I'm glad to know that the next version will allow this syntax.
thanks again to both


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

Share this post


Link to post
Share on other sites
jchd

In addition you will be able to use indexing directly on the array returned by a function: ConsoleWrite("abc" & myfunc($param)[2] & @LF)

But of course you want to use such construct only when the probability is = 1 that myfunc returns a result incompatible with [2] index.


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)

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  

×