ChrisEnn

Declaring and initializing multi-dimension arrays

9 posts in this topic

#1 ·  Posted (edited)

Hi,

This is my first post here. I've searched through the help and these forums and haven't found what I'm looking for.

Although I have quite a bit of experience in programming, it's all quite a long time ago (COBOL and dBase/Clipper), and this is my first attempt to write something quite complex using AutoIt.

I'm writing an app that reads a single CSV file and then splits it into a number of separate arrays based on the 
sections in the original file. I then display each of the individual arrays in a ListView control.

In order to standardize my code I would like to declare the structure that each array/listview will have, by creating a
structure array that contains the following elements: 
- section name,
- number of columns,
- an array with the column headers,
- an array with the column widths

The last two sub-arrays will have a variable number of entries, equal to the second value, the number of columns.
Unfortunately, I can't get this to work, I keep getting syntax errors.

This is my structure array at present (note that the widths are just placeholders at present) :

Global $aStructures[10][4] = [_
["params",         3, ["parameter", "value", "comments"], [110, 150, 450] ], _
["notes",          1, ["note"], [500] ], _
["sizes",          8, ["name", "width", "height", "blockwidth", "blockheight", "hgridsize", "vgridsize", "comments"], [10, 10, 10, 10, 10, 10, 10, 10] ], _
["templates",      2, ["name", "comments"], [10, 10] ], _
["elements",      10, ["template", "name", "nature", "frontback", "left", "top", "width", "height", "font", "size"], [10, 10, 10, 10, 10, 10, 10, 10 10, 10] ], _
["schemes",        4, ["name", "frontbg", "backbg", "comments"], [10, 10, 10, 10] ], _
["elementcolours", 7, ["scheme", "element", "fg", "bg", "bg2", "gradtype", "gradval"], [10, 10, 10, 10, 10, 10, 10] ], _
["colours",        7, ["name", "r", "g", "b", "rgb", "hex", "sample"], [10, 10, 10, 10, 10, 10, 10] ], _
["sheets",         2, ["name", "comments"], [10, 10] ], _
["items",         10, ["sheet", "size", "template", "scheme", "qty", "f1", "f2", "f3", "f4", "f5"], [10, 10, 10, 10, 10, 10, 10, 10, 10, 10] ] _
]


So, two questions.

1/ Are such arrays allowed, with a variable number of dimensions ?
2/ If so, what am I doing wrong in the above code ?

Any help would be welcome.

Chris

 

Edited by ChrisEnn
removed code that wasn't between code tags

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Variable dimensions aren't supported in AutoIt. You'll need to add dummy entries and possibly a count to hold the maximum dimension(s).

Edited by jchd

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

You could maybe do it like this:

Global $aStructures = [ _
[["params"],         [3],  ["parameter", "value", "comments"],                                                             [110, 150, 450]] , _
[["notes"],          [1],  ["note"],                                                                                       [500]] , _
[["sizes"],          [8],  ["name", "width", "height", "blockwidth", "blockheight", "hgridsize", "vgridsize", "comments"], [10, 10, 10, 10, 10, 10, 10, 10]] , _
[["templates"],      [2],  ["name", "comments"],                                                                           [10, 10]] , _
[["elements"],       [10], ["template", "name", "nature", "frontback", "left", "top", "width", "height", "font", "size"],  [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]] , _
[["schemes"],        [4],  ["name", "frontbg", "backbg", "comments"],                                                      [10, 10, 10, 10]] , _
[["elementcolours"], [7],  ["scheme", "element", "fg", "bg", "bg2", "gradtype", "gradval"],                                [10, 10, 10, 10, 10, 10, 10]] , _
[["colours"],        [7],  ["name", "r", "g", "b", "rgb", "hex", "sample"],                                                [10, 10, 10, 10, 10, 10, 10]] , _
[["sheets"],         [2],  ["name", "comments"],                                                                           [10, 10]] , _
[["items"],          [10], ["sheet", "size", "template", "scheme", "qty", "f1", "f2", "f3", "f4", "f5"],                   [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]]  _
]

For $Row = 0 To UBound($aStructures) - 1
    ConsoleWrite($aStructures[$Row][0][0] & @CRLF)

    For $i = 0 To $aStructures[$Row][1][0] - 1
        ConsoleWrite(@TAB & $aStructures[$Row][2][$i])
        ConsoleWrite(" = " & $aStructures[$Row][3][$i] & @CRLF)
    Next

    ConsoleWrite(@CRLF)
Next

 

1 person likes this

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Could use a json UDF

#include <GUIConstants.au3>
#include <WinApi.au3>
#include "Json.au3"

Global $sJsonString = '{"structure":{"params":[3,["parameter","value","comments"],[110,150,450]],"notes":[1,["note"],[500]],"sizes":[8,["name","width","height","blockwidth","blockheight","hgridsize","vgridsize","comments"],[10,10,10,10,10,10,10,10]],"templates":[2,["name","comments"],[10,10]],"elements":[10,["template","name","nature","frontback","left","top","width","height","font","size"],[10,10,10,10,10,10,10,10,10,10]],"schemes":[7,["scheme","element","fg","bg","bg2","gradtype","gradval"],[10,10,10,10,10,10,10]],"sheets":[2,["name","comments"],[10,10]],"colours":[10,["sheet","size","template","scheme","qty","f1","f2","f3","f4","f5"],[10,10,10,10,10,10,10,10,10,10]]}}'
Global $oJson = Json_Decode($sJsonString)
Global $hGui = GUICreate("Json Example", 800, 620)
Global $hEditJson = GUICtrlCreateEdit(Json_Encode($oJson) & @CRLF & Json_Encode($oJson, $JSON_PRETTY_PRINT, "\t", ",\r\n", "\r\n", ":"), 10, 10, 780, 280)
Global $hEditOut = GUICtrlCreateEdit("", 10, 330, 780, 280)
Global $hInput = GUICtrlCreateInput(".structure.sizes", 10, 300, 200, 20)
GUICtrlSetFont($hEditJson, 10, 400, 0, "Consolas")
GUICtrlSetFont($hEditOut, 10, 400, 0, "Consolas")
GUICtrlSetFont($hInput, 10, 400, 0, "Consolas")

GUISetState(@SW_SHOW, $hGui)

PrintStructure($oJson, ".structure.sizes")

GUIRegisterMsg($WM_COMMAND, WM_COMMAND)

While (True)
    Switch (GUIGetMsg())
        Case $GUI_EVENT_CLOSE
            GUIDelete($hGui)
            Exit 0
    EndSwitch
WEnd

Func PrintStructure(ByRef $Object, Const ByRef $sKey)
    Local $iParams = Json_Get($oJson, $sKey & "[0]")
    GUICtrlSetData($hEditOut, $iParams & " params for key " & $sKey & @CRLF)
    For $i = 0 To $iParams - 1
        GUICtrlSetData($hEditOut, GUICtrlRead($hEditOut) & $sKey & "[1][" & $i & "] = " & Json_Get($oJson, $sKey & "[1][" & $i & "]") & @CRLF)
        GUICtrlSetData($hEditOut, GUICtrlRead($hEditOut) & $sKey & "[2][" & $i & "] = " & Json_Get($oJson, $sKey & "[2][" & $i & "]") & ($i = $iParams -1 ? "" : @CRLF))
    Next
EndFunc   ;==>PrintStructure

Func WM_COMMAND($hWndFrom, $iMsg, $wParam, $lParam)
    Local $iCode = _WinAPI_HiWord($wParam)
    Local $iIDFrom = _WinAPI_LoWord ($wParam)
    Local $sKey = ""
    Local $sJson = ""
    Local $sJsonValue = ""

    Switch $iIDFrom
        Case $hInput
            Switch $iCode
                Case $EN_CHANGE
                    $sKey = StringRegExpReplace(GUICtrlRead($iIDFrom), "(?i)\.|structure", "")
                    $sJson = Json_Encode($oJson)
                    $sJsonValue = Json_Get($oJson, ".structure." & $sKey)
                    If (StringInStr($sJson, '"' & $sKey & '"')) Then
                        PrintStructure($oJson, ".structure." & $sKey)
                    ElseIf (StringRegExp($sKey, "\[|\]") and StringLen($sJsonValue)) Then
                        GUICtrlSetData($hEditOut, "Value at key '" & ".structure." & $sKey & "' = " & $sJsonValue)
                    EndIf
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

 

Share this post


Link to post
Share on other sites

Thanks for your answers.

I'd thought of using JSON rather than CSV, and haven't yet made a definite decision on what format to use, but my existing files are CSV from Excel (I'm porting an existing Excel-VBA app to a standalone executable), so initially it just seemed easier to use _FileReadToArray and _FileWriteFromArray.

@trancexx: that seems exactly what I want, I'll use your solution for now, many thanks.

 

Chris

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

@InonuTaishou: Your reply is interesting too, my initial reply about JSON was for my data files, but I could indeed use this for my structure array. I'll go with trancess's idea for now, as it seems easier to implement.

Edited by ChrisEnn
typo

Share this post


Link to post
Share on other sites

Another newbie question.

Using trancexx's solution above, how can I retrieve one of the sub-arrays.

This works :

Local $vVar = $aStructures[0][2][0] ; => "parameter"

but this doesn't :

Local $aArray = $aStructures[0][2]  ; => ["parameter", "value", "comments"]

Instead of retrieving the subarray, I get "Array variable has incorrect number of subscripts or subscript dimension range exceeded."

 

How can I retrieve a sub-array in one go, to then pass it as a parameter to a function ?

Thanks for your help.

Share this post


Link to post
Share on other sites

That's called slicing. Write a function for it in which you'd copy elements from the original array into new one. There's no buit-in mechanism for it.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Ok, I'll do that. Thanks for your quick reply.

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

  • Similar Content

    • 9252Survive
      By 9252Survive
      Hi All, 
       
      I am fairly new to AutoIT and I am still trying to learn, I have been using _FileListToArray to list all the files with a particular extension in an array and then loop through it for operation  (   For $i = 1 To UBound($FileArray) - 1).
      So far this has been working fine. But I am not able to figure out a problem that I have; what if I have 50 files but I only want to loop through first 10 files and then next ten and so on?  Or rather I should say, how I can I only feed max 10 files to the array at a time when I do _FileListToArray regardless of the total number of files in the folder?
      Any insight/help will be much appreciated 
    • NiftRex
      By NiftRex
      I'm trying to get an array from a website so that I can just get the url, but I am not sure how. I read a bit of arrays but I have a feeling I'd have to be writing a lot more than what I should be. I will include the script I have so far and the API url for what I want.
       
      API: https://api.fast.com/netflix/speedtest?https=true&token=YXNkZmFzZGxmbnNkYWZoYXNkZmhrYWxm&urlCount=1 (I want the 'url' array that contains the url)
       
      Code:
      #include <MsgBoxConstants.au3> #include <Inet.au3> #include <Array.au3> $site = _INetGetSource('http://api.fast.com/netflix/speedtest?https=true&token=YXNkZmFzZGxmbnNkYWZoYXNkZmhrYWxm&urlCount=1') MsgBox($MB_SYSTEMMODAL, "Title", $site[1])  
    • rcmaehl
      By rcmaehl
      Alright, currently I'm trying to merge two loops:
       
      Local $aArray[99][13] For $iLoop = 1 to 100 $aArray[$iLoop - 1][0] = RegEnumKey("HKLM\SYSTEM\CurrentControlSet\Control\GraphicsDrivers\Configuration", $iLoop) If @error <> 0 Then ReDim $aArray[$iLoop - 1][13] ExitLoop EndIf Next For $iLoop1 = 0 to UBound($aArray, 1) - 1 For $iLoop2 = 1 to 12 $aArray[$iLoop1][$iLoop2] = RegEnumKey("HKLM\SYSTEM\CurrentControlSet\Control\GraphicsDrivers\Configuration\" & $aArray[$iLoop1][0], $iLoop2) If @error <> 0 Then ExitLoop Next Next  
      into one. Here's what I have so far:
       
      Local $aArray[99][13] For $iLoop1 = 0 to UBound($aArray, 1) - 1 For $iLoop2 = 0 to 12 ConsoleWrite("Loop1 = " & $iLoop1 & ", Loop2 = " & $iLoop2 & @CRLF) If $iLoop2 = 0 Then $aArray[$iLoop1][0] = RegEnumKey("HKLM\SYSTEM\CurrentControlSet\Control\GraphicsDrivers\Configuration", $iLoop1 + 1) If @error <> 0 Then ReDim $aArray[$iLoop1 + 1][13] ExitLoop EndIf Else $aArray[$iLoop1][$iLoop2] = RegEnumKey("HKLM\SYSTEM\CurrentControlSet\Control\GraphicsDrivers\Configuration\" & $aArray[$iLoop1][0], $iLoop2) If @error <> 0 Then ExitLoop EndIf Next Next And it keeps error-ing out trying to exceed the dimension range of the array. Does the Loop not recheck the size of the array after the original check or am I doing something else wrong.
       
      Thanks.
    • FrancescoDiMuro
      By FrancescoDiMuro
      Hi guys! How are you? Hope you're fine
      I'm trying to use SQLite for managing some data, and, I would like to display my "retrieved" data, but I'm trying to do a select from 2 table, and seems to not work properly...
      What I'd like to do is retrieve data from 2 tables and display in a listview...
      I tried with:
      Local $aRisultato, $iRighe, $iColonne, $iRVal $iRval = _SQLite_GetTable2d($hDatabase, "SELECT * FROM Magazzino_Rockwell, Magazzino_Siemens;", $aRisultato, $iRighe, $iColonne) If $iRVal = $SQLITE_OK Then ;_SQLite_Display2DResult($aRisultato) _ArrayDisplay($aRisultato) EndIf ... but it displays a single record 41 times, and it does this thing for every record in the database...
      What do you need to help me?
      Thanks  

      EDIT:
      Managed with a double If and double query like this:
       
      $iRval = _SQLite_GetTable2d($hDatabase, "SELECT * FROM Magazzino_Rockwell;", $aRisultato, $iRighe, $iColonne) If $iRVal = $SQLITE_OK Then Local $aRisultatoRockwell = $aRisultato $iRval = _SQLite_GetTable2d($hDatabase, "SELECT * FROM Magazzino_Siemens;", $aRisultato, $iRighe, $iColonne) If $iRVal = $SQLITE_OK Then _ArrayConcatenate($aRisultato, $aRisultatoRockwell) Local $i, $sRiga, $s_LV_Item For $i = 1 To Ubound($aRisultato) - 1 $sRiga = $aRisultato[$i][0] & "|" & _ $aRisultato[$i][1] & "|" & _ $aRisultato[$i][2] & "|" & _ $aRisultato[$i][3] & "|" & _ $aRisultato[$i][4] & "|" & _ $aRisultato[$i][5] & "|" & _ $aRisultato[$i][6] & "|" & _ $aRisultato[$i][7] & "|" & _ $aRisultato[$i][8] & "|" & _ $aRisultato[$i][9] & "|" & _ $aRisultato[$i][10] & "|" & _ $aRisultato[$i][11] $s_LV_Item = GUICtrlCreateListViewItem($sRiga, $lv_Lista) Next And so, the listview is created
      If anyone has another more efficient way, I'm here Thanks guys  
       
    • TheAutomator
      By TheAutomator
      Hi people,
      It's been a while
      I have been scripting in vbscript for some time but now i'm back using AutoIt for a project:
      Scripting Adobe Illustrator with AutoIt .
      This vb code needs to be converted:
      Set appRef = CreateObject("Illustrator.Application") 'Create a new document and assign it to a variable Set documentRef = appRef.Documents.Add 'Create a new text frame item and assign it to a variable Set sampleText = documentRef.TextFrames.Add 'Set the contents and position of the TextFrame sampleText.Position = Array(200, 200) sampleText.Contents = "Hello World!" The ... = Array(200,200) part is done in one line in vb, is this possible in AutoIt without declaring an array first?
      I know I sound like a newbie but I never had to use arrays this way before..
      what i have:
      $appRef = ObjCreate("Illustrator.Application") ;Create a new document and assign it to a variable $documentRef = $appRef.Documents.Add ;Create a new text frame item and assign it to a variable $sampleText = $documentRef.TextFrames.Add ;Set the contents and position of the TextFrame ReDim $sampleText.Position[2] = [200, 200] <<<--- doesn't work... $sampleText.Contents = "Hello World!"