Jump to content

General question upon array declaration


rudi
 Share

Recommended Posts

Hi.

 

In this post by @mistersquirrle I saw again the possibility to declare array variables without specifying the exact dimension values -- I've seen it here before, but didn't pay special attention to it.

IIRC this is new with feature with Autoit v.3.16.1, isn't it?

Well, in the help file I didn't find a dedicated topic covering this way to declare arrays -- probably I missed it?

 

  • Is there any difference between an array that's declared with exact dimension sizes specified, compared to this way just using "[]" ? Investigating the array I at least cannot notice any difference?
  • If there is a difference, what exatly?
  • pros / cons to use this way to declare arrays?

 

#include <Debug.au3>

Global $Liste = ["nine", "ten", "eleven", "twelve"]
ArrayDetails($Liste)
_DebugArrayDisplay($Liste)

Global $Liste[][] = [["neun", "nine"], ["zehn", "ten"], ["elf", "eleven"], ["zwölf", "twelve"]]
ArrayDetails($Liste)
_DebugArrayDisplay($Liste)

Global $Liste[][][] = [[["neun", "nine"], ["zehn", "ten"], ["elf", "eleven"], ["zwölf", "twelve"]], _
        [["1101", "9"], ["1010", "A"], ["1011", "B"], ["1100", "C"]]]
ArrayDetails($Liste)

Func ArrayDetails($Liste)
    ConsoleWrite("-------------" & @CRLF)
    ConsoleWrite("Var Type  : " & VarGetType($Liste) & @CRLF)
    ConsoleWrite("Dimensions: " & UBound($Liste, 0) & @CRLF)
    ConsoleWrite("Rows      : " & UBound($Liste, 1) & @CRLF)
    ConsoleWrite("Columns   : " & UBound($Liste, 2) & @CRLF)
EndFunc   ;==>ArrayDetails

 

>Running:(3.3.16.1):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\temp\löschmich\array-test.au3"    
+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.
-------------
Var Type  : Array
Dimensions: 1
Rows      : 4
Columns   : 0
-------------
Var Type  : Array
Dimensions: 2
Rows      : 4
Columns   : 2
-------------
Var Type  : Array
Dimensions: 3
Rows      : 2
Columns   : 4
+>19:35:13 AutoIt3.exe ended.rc:0
+>19:35:13 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 42.08

I've made use of 3D arrays before without taking care, what's looked at as column and what as row, as it doesn't matter at all to address the "cell-in-the-cube" you need: The output shows, that somewhat "rows and columns" swap, when going 3D? 

 

TIA, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

22 minutes ago, rudi said:

Well, in the help file I didn't find a dedicated topic covering this way to declare arrays -- probably I missed it?

  • Is there any difference between an array that's declared with exact dimension sizes specified, compared to this way just using "[]" ? Investigating the array I at least cannot notice any difference?
  • If there is a difference, what exatly?
  • pros / cons to use this way to declare arrays?

Read Help > Language Reference - Variables > Arrays and Maps

You declare a Map as Local $mMyMap[] without assignment attached.

When you add an assignment, you're declaring an array, not a Map. But I recommend against using [][]...[] since this part is pointless unless you provide explicit dimensions.

Change your function to this:

Func ArrayDetails($Liste)
    ConsoleWrite("-------------" & @CRLF)
    ConsoleWrite("Var Type  : " & VarGetType($Liste) & @CRLF)
    ConsoleWrite("Dimensions: " & UBound($Liste, 0) & @CRLF)
    ConsoleWrite("Rows      : " & UBound($Liste, 1) & @CRLF)
    ConsoleWrite("Columns   : " & UBound($Liste, 2) & @CRLF)
    ConsoleWrite("Planes    : " & UBound($Liste, 3) & @CRLF)
EndFunc   ;==>ArrayDetails

Another view of your 3D array, using a homebrew "Variable Dump" function:

Array[2][4][2]
      [0][0][0] => String(4)          'neun'
      [0][0][1] => String(4)          'nine'
      [0][1][0] => String(4)          'zehn'
      [0][1][1] => String(3)          'ten'
      [0][2][0] => String(3)          'elf'
      [0][2][1] => String(6)          'eleven'
      [0][3][0] => String(5)          'zwölf'
      [0][3][1] => String(6)          'twelve'
      [1][0][0] => String(4)          '1101'
      [1][0][1] => String(1)          '9'
      [1][1][0] => String(4)          '1010'
      [1][1][1] => String(1)          'A'
      [1][2][0] => String(4)          '1011'
      [1][2][1] => String(1)          'B'
      [1][3][0] => String(4)          '1100'
      [1][3][1] => String(1)          'C'

There is no such thing as "array dimensions mixup", 3D or 12D.

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

Another example where every dimension is unique:

Global $Liste = _
[ _
    [ _
        ['000', '001', '002', '003'], _
        ['010', '011', '012', '013'], _
        ['020', '021', '022', '023'] _
    ], _
    [ _
        ['100', '101', '102', '103'], _
        ['110', '111', '112', '113'], _
        ['120', '121', '122', '123'] _
    ] _
]
ArrayDetails($Liste)
VD($Liste, 1)     ; dump all entries

Func ArrayDetails($Liste)
    ConsoleWrite("-------------" & @CRLF)
    ConsoleWrite("Var Type  : " & VarGetType($Liste) & @CRLF)
    ConsoleWrite("Dimensions: " & UBound($Liste, 0) & @CRLF)
    ConsoleWrite("Rows      : " & UBound($Liste, 1) & @CRLF)
    ConsoleWrite("Columns   : " & UBound($Liste, 2) & @CRLF)
    ConsoleWrite("Planes    : " & UBound($Liste, 3) & @CRLF)
EndFunc   ;==>ArrayDetails

Results in:

Var Type  : Array
Dimensions: 3
Rows      : 2
Columns   : 3
Planes    : 4
Array[2][3][4]
      [0][0][0] => String(3)          '000'
      [0][0][1] => String(3)          '001'
      [0][0][2] => String(3)          '002'
      [0][0][3] => String(3)          '003'
      [0][1][0] => String(3)          '010'
      [0][1][1] => String(3)          '011'
      [0][1][2] => String(3)          '012'
      [0][1][3] => String(3)          '013'
      [0][2][0] => String(3)          '020'
      [0][2][1] => String(3)          '021'
      [0][2][2] => String(3)          '022'
      [0][2][3] => String(3)          '023'
      [1][0][0] => String(3)          '100'
      [1][0][1] => String(3)          '101'
      [1][0][2] => String(3)          '102'
      [1][0][3] => String(3)          '103'
      [1][1][0] => String(3)          '110'
      [1][1][1] => String(3)          '111'
      [1][1][2] => String(3)          '112'
      [1][1][3] => String(3)          '113'
      [1][2][0] => String(3)          '120'
      [1][2][1] => String(3)          '121'
      [1][2][2] => String(3)          '122'
      [1][2][3] => String(3)          '123'

Yet, I find it more intuitive to name 3D dimensions in the reverse order: planes, rows, columns.

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

...and by the way any array with multiple dimensions can be reduced essentially it's a 1D array...

link with simple explanation: https://codegolf.stackexchange.com/questions/79609/index-of-a-multidimensional-array

p.s.
just a small hijacking digression on the subject

Edited by Gianni

 

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

jchd has some great examples, but here's the helpfile piece that you may have missed (as @jchd said Read Help > Language Reference - Variables > Arrays and Maps)https://www.autoitscript.com/autoit3/docs/intro/lang_variables.htm#ArrayMaps

As mentioned, you're mixing up Arrays and Maps, which are two different things. Maps were indeed added in the latest stable release of AutoIt, but have been around for a while in beta.

In your example however you are only creating Arrays, to create a map you cannot add any data to it when declaring, it has to be just a plain and simple:

Local $mMap[] ; If you specify any data at declaration, it's interpreted as an array

The key difference (for most people) between Arrays and Maps is that Arrays are accessed by an index, and Maps by a key. That key can be numeric, but it is not an index. There's a few Map* functions to assist with using them: https://www.autoitscript.com/autoit3/docs/functions/Map Management.htm, though using a map is as simple as:

Global $mMap[] ; Create your map
$mMap.hTimer = TimerInit() ; Creates a new key and sets the value
Sleep(500)
ConsoleWrite(Round(TimerDiff($mMap.hTimer), 2) & ' ms' & @CRLF)
$mMap.sMsg = 'This is a message, here are the numbers 0-9: ' ; Another new key

$mMap.iLoopMax = 10
$mMap.hTimer = TimerInit() ; Update value, start new timer
For $i = 0 To $mMap.iLoopMax - 1
    $mMap.sMsg &= $i & ', '
    Sleep(10)
Next
$mMap.sMsg = StringTrimRight($mMap.sMsg, 2)

ConsoleWrite('sMsg: ' & $mMap.sMsg & ', took: ' & Round(TimerDiff($mMap.hTimer), 2) & ' ms' & @CRLF)
ConsoleWrite("You can also access map keys using ['sKey'] syntax: " & $mMap['sMsg'] & ', took: ' & Round(TimerDiff($mMap['hTimer']), 2) & ' ms' & @CRLF)

For $i = 0 To $mMap.iLoopMax - 1
    $mMap[$i] = Random(0, 9, 1) ; Creating new keys that are numeric, there are not indexes however
Next

For $i = 0 To $mMap['iLoopMax'] - 1
    ConsoleWrite('$mMap[' & $i & ']: ' & $mMap[$i] & @CRLF)
Next

; There are couple ways to list everything in the map
Global $aMapKeys = MapKeys($mMap)
For $iKey = 0 to UBound($aMapKeys) - 1
    ConsoleWrite('$aMapKeys[$iKey] - Key: ' & $aMapKeys[$iKey] & ', value: ' & $mMap[$aMapKeys[$iKey]] & @CRLF)
Next

; Or this way which just returns the data, but no reference of the key (unless I'm missing something), so MapKeys is preferred
For $vValue In $mMap
    ConsoleWrite('$vValue In $mMap - Value: ' & $vValue & @CRLF)
Next

If you want to learn more about AutoIt's Maps, I would recommend this page: 

While it's quite old now and is about Maps when they were still a beta feature, it's still got some good information.

 

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

5 hours ago, Gianni said:

...and by the way any array with multiple dimensions can be reduced essentially it's a 1D array...

Not in AutoIt!  As explained in the cited post in C/C++ and other languages, an array is just allocating a continuous block (a number of entries) where you play games with indices to get where you need to, based on the once-for-all fixed size of every entry and base pointer.

AutoIt arrays have no such linear structure accessible to the programmer. It isn't obvious to me that every entry in an AutoIt array has the same size, regardless of array dimension(s) and you have no knowledge of the array "base pointer" (provided there is one).

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

Thanks to all repliers!
 

Youre right, that's the part of the docu I've missed: https://www.autoitscript.com/autoit3/docs/intro/lang_variables.htm#ArrayMaps

In autoit I didn't use maps a lot so far, but within powershell I frequently use maps, wich are called hash tables in powershell: $hMyHash=@{}, (curled brackets), as the results are returned literally instantly, even for very large hash tables.

 

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Here's the code. It also needs another piece (CW) used to display Unicode on the console.

Be warned: very raw, not much comments!

Dump.au3 CW.au3

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

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