Popular Post Gianni Posted 5 hours ago Popular Post Posted 5 hours ago (edited) A simple _MAPdisplay() function: displays the contents of a MAP in a TreeView handles nested MAPs if the MAP contains an array (1D or 2D), it displays the contents in a draft form; for arrays greater than 2D, the array is indicated but not expanded If an array element contains a sub-MAP, a sub-array or other "non printable" variables, it is indicated the VarType but not recursively expanded This is the UDF (2 functions). Below there is a second script with examples expandcollapse popup#include <TreeViewConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <Array.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: _MapDisplay ; Description ...: ; Syntax ........: _MapDisplay(Byref $mMain) ; Parameters ....: $mMain - a map. ; Return values .: None ; Author ........: Gianni Addiego ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _MapDisplay(ByRef $mMain, $bPseudoParent = False) If Not IsMap($mMain) Then Return SetError(1, 0, '') ; Create and show the GUI Local $hGUI = GUICreate("Map Display", 850, 550) Local $idTreeView = GUICtrlCreateTreeView(10, 10, 830, 530, $TVS_HASBUTTONS + $TVS_HASLINES + $TVS_LINESATROOT, $WS_EX_CLIENTEDGE) GUICtrlSetFont($idTreeView, 8.5, 0, 0, "Courier New") GUISetState(@SW_SHOW) If $bPseudoParent Then ; (virtual root) Local $__mPseudoRoot[] $__mPseudoRoot['Root'] = $mMain _MapToTreeView($__mPseudoRoot, $idTreeView) Else _MapToTreeView($mMain, $idTreeView) EndIf ; Waiting loop Do Until GUIGetMsg() = $GUI_EVENT_CLOSE GUIDelete($hGUI) $idTreeView = 0 Return EndFunc ;==>_MapDisplay ; Ebhanced recursive function Func _MapToTreeView(ByRef $map, $idParent) Local $aKeys = MapKeys($map) Local $key, $value, $idNewParentNode Local $iDimensions, $idNodeArray1D, $idNodeArray2D Local $iRows, $iColumns, $sRowText, $vCheckType For $i = 0 To UBound($aKeys) - 1 $key = $aKeys[$i] $value = $map[$key] ; Nested MAP If IsMap($value) Then $idNewParentNode = GUICtrlCreateTreeViewItem("[" & $key & "] (MAP)", $idParent) _MapToTreeView($value, $idNewParentNode) ; Array (1D or 2D) ElseIf IsArray($value) Then ; Check the number of array dimensions $iDimensions = UBound($value, $UBOUND_DIMENSIONS) If $iDimensions = 1 Then ; 1D Array Management $idNodeArray1D = GUICtrlCreateTreeViewItem("[" & $key & "] (1D ARRAY)", $idParent) For $j = 0 To UBound($value) - 1 $vCheckType = IsArray($value[$j]) ? '{Array}' _ : IsMap($value[$j]) ? '{MAP}' _ : IsObj($value[$j]) ? '{Object}' _ : IsDllStruct($value[$j]) ? '{DLLStruct}' _ : IsFunc($value[$j]) ? '{Function}' _ : $value[$j] GUICtrlCreateTreeViewItem("[" & $j & "]: " & $vCheckType, $idNodeArray1D) Next ElseIf $iDimensions = 2 Then ; 2D Array Management (Horizontal Columns) $idNodeArray2D = GUICtrlCreateTreeViewItem("[" & $key & "] (2D ARRAY)", $idParent) $iRows = UBound($value, $UBOUND_ROWS) $iColumns = UBound($value, $UBOUND_COLUMNS) For $r = 0 To $iRows - 1 $sRowText = "Row " & $r & " -> " ; Loop through columns and concatenate them horizontally For $c = 0 To $iColumns - 1 ; $vCheckType = IsArray($value[$r][$c]) ? '{Array}' : IsMap($value[$r][$c]) ? '{MAP}' : $value[$r][$c] $vCheckType = IsArray($value[$r][$c]) ? '{Array}' _ : IsMap($value[$r][$c]) ? '{MAP}' _ : IsObj($value[$r][$c]) ? '{Object}' _ : IsDllStruct($value[$r][$c]) ? '{DLLStruct}' _ : IsFunc($value[$r][$c]) ? '{Function}' _ : $value[$r][$c] $sRowText &= "[" & $vCheckType & "]" ; $sRowText &= $vCheckType If $c < $iColumns - 1 Then $sRowText &= " | " ; Separator between columns Next ; Create a single item for the entire row GUICtrlCreateTreeViewItem($sRowText, $idNodeArray2D) Next Else ; Arrays with 3 or more dimensions (uncommon, showing a warning only) GUICtrlCreateTreeViewItem("[" & $key & "] (" & $iDimensions & "D ARRAY)", $idParent) ; Unsupported EndIf Else ; Standard value $vCheckType = IsArray($value) ? '{Array}' _ : IsMap($value) ? '{MAP}' _ : IsObj($value) ? '{Object}' _ : IsDllStruct($value) ? '{DLLStruct}' _ : IsFunc($value) ? '{Function}' _ : $value GUICtrlCreateTreeViewItem($key & ": " & $vCheckType, $idParent) ; place a separetor between key and data (:) ; GUICtrlCreateTreeViewItem($key & " " & $vCheckType, $idParent) ; without separetor EndIf Next EndFunc ;==>_MapToTreeView This second script contains the UDF as well as two examples that generate two fairly complex maps and display them. expandcollapse popup#include <TreeViewConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <Array.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: _MapDisplay ; Description ...: ; Syntax ........: _MapDisplay(Byref $mMain) ; Parameters ....: $mMain - a map. ; Return values .: None ; Author ........: Gianni Addiego ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _MapDisplay(ByRef $mMain, $bPseudoParent = False) If Not IsMap($mMain) Then Return SetError(1, 0, '') ; Create and show the GUI Local $hGUI = GUICreate("Map Display", 850, 550) Local $idTreeView = GUICtrlCreateTreeView(10, 10, 830, 530, $TVS_HASBUTTONS + $TVS_HASLINES + $TVS_LINESATROOT, $WS_EX_CLIENTEDGE) GUICtrlSetFont($idTreeView, 8.5, 0, 0, "Courier New") GUISetState(@SW_SHOW) If $bPseudoParent Then ; (virtual root) Local $__mPseudoRoot[] $__mPseudoRoot['Root'] = $mMain _MapToTreeView($__mPseudoRoot, $idTreeView) Else _MapToTreeView($mMain, $idTreeView) EndIf ; Waiting loop Do Until GUIGetMsg() = $GUI_EVENT_CLOSE GUIDelete($hGUI) $idTreeView = 0 Return EndFunc ;==>_MapDisplay ; Ebhanced recursive function Func _MapToTreeView(ByRef $map, $idParent) Local $aKeys = MapKeys($map) Local $key, $value, $idNewParentNode Local $iDimensions, $idNodeArray1D, $idNodeArray2D Local $iRows, $iColumns, $sRowText, $vCheckType For $i = 0 To UBound($aKeys) - 1 $key = $aKeys[$i] $value = $map[$key] ; Nested MAP If IsMap($value) Then $idNewParentNode = GUICtrlCreateTreeViewItem("[" & $key & "] (MAP)", $idParent) _MapToTreeView($value, $idNewParentNode) ; Array (1D or 2D) ElseIf IsArray($value) Then ; Check the number of array dimensions $iDimensions = UBound($value, $UBOUND_DIMENSIONS) If $iDimensions = 1 Then ; 1D Array Management $idNodeArray1D = GUICtrlCreateTreeViewItem("[" & $key & "] (1D ARRAY)", $idParent) For $j = 0 To UBound($value) - 1 $vCheckType = IsArray($value[$j]) ? '{Array}' _ : IsMap($value[$j]) ? '{MAP}' _ : IsObj($value[$j]) ? '{Object}' _ : IsDllStruct($value[$j]) ? '{DLLStruct}' _ : IsFunc($value[$j]) ? '{Function}' _ : $value[$j] GUICtrlCreateTreeViewItem("[" & $j & "]: " & $vCheckType, $idNodeArray1D) Next ElseIf $iDimensions = 2 Then ; 2D Array Management (Horizontal Columns) $idNodeArray2D = GUICtrlCreateTreeViewItem("[" & $key & "] (2D ARRAY)", $idParent) $iRows = UBound($value, $UBOUND_ROWS) $iColumns = UBound($value, $UBOUND_COLUMNS) For $r = 0 To $iRows - 1 $sRowText = "Row " & $r & " -> " ; Loop through columns and concatenate them horizontally For $c = 0 To $iColumns - 1 ; $vCheckType = IsArray($value[$r][$c]) ? '{Array}' : IsMap($value[$r][$c]) ? '{MAP}' : $value[$r][$c] $vCheckType = IsArray($value[$r][$c]) ? '{Array}' _ : IsMap($value[$r][$c]) ? '{MAP}' _ : IsObj($value[$r][$c]) ? '{Object}' _ : IsDllStruct($value[$r][$c]) ? '{DLLStruct}' _ : IsFunc($value[$r][$c]) ? '{Function}' _ : $value[$r][$c] $sRowText &= "[" & $vCheckType & "]" ; $sRowText &= $vCheckType If $c < $iColumns - 1 Then $sRowText &= " | " ; Separator between columns Next ; Create a single item for the entire row GUICtrlCreateTreeViewItem($sRowText, $idNodeArray2D) Next Else ; Arrays with 3 or more dimensions (uncommon, showing a warning only) GUICtrlCreateTreeViewItem("[" & $key & "] (" & $iDimensions & "D ARRAY)", $idParent) ; Unsupported EndIf Else ; Standard value $vCheckType = IsArray($value) ? '{Array}' _ : IsMap($value) ? '{MAP}' _ : IsObj($value) ? '{Object}' _ : IsDllStruct($value) ? '{DLLStruct}' _ : IsFunc($value) ? '{Function}' _ : $value GUICtrlCreateTreeViewItem($key & ": " & $vCheckType, $idParent) ; place a separetor between key and data (:) ; GUICtrlCreateTreeViewItem($key & " " & $vCheckType, $idParent) ; without separetor EndIf Next EndFunc ;==>_MapToTreeView ; ----- Example zone ----- #include <date.au3> #include <InetConstants.au3> ; The functions Example1 and Example2 simply generate a rather complex MAP. ; Both returned MAPs are displayed by the _MapDisplay() function. _MapDisplay(Example1()) _MapDisplay(Example2()) Func Example1() ; --- Example 1 --- ; Create a MAP containing nested MAPs ; Read a file CSV containing european States, Regions, Provinces ; Read the file directly to a variable without saving it to a local file. Local $dData = InetRead("https://gisco-services.ec.europa.eu/distribution/v2/nuts/csv/NUTS_AT_2024.csv", BitAND($INET_FORCERELOAD, $INET_BINARYTRANSFER)) ; Convert the ANSI compatible binary string back into a string. Local $sData = BinaryToString($dData, $SB_UTF8) ; $SB_UTF8 (4) = binary data is UTF8 Local $a2D = _StringToArray2D($sData, ',') ; convert a CSV into a two-dimensional array ; _ArrayDisplay($a2D) Local $mContinents[] ; only Europe in this context Local $mCountries[] Local $mRegions[] ; Local $mProvinces[] Local $mMAP[] ; an empty MAP For $i = 1 To UBound($a2D) - 1 $a2D[$i][4] = StringLen($a2D[$i][1]) & $a2D[$i][2] $a2D[$i][5] = StringLeft($a2D[$i][1], 4) Next _ArraySort($a2D, 0, 1, 0, 4) ; create a nested MAP : Continents -> Countries -> Regions -> Provinces $mContinents['Europe'] = $mMAP ; first submap For $i = 1 To UBound($a2D) - 1 Switch StringLen($a2D[$i][1]) Case 2 ; State code $mContinents["Europe"][$a2D[$i][2]] = $mMAP $mCountries[$a2D[$i][1]] = $a2D[$i][2] ; <- populate the map of Countries Case 4 ; Region code $mRegions[$a2D[$i][1]] = $a2D[$i][2] ; <- populate the map of Regions ; We place the region within the map of its State $mContinents["Europe"][$mCountries[$a2D[$i][0]]][$a2D[$i][2]] = $mMAP Case 5 ; Province code ; We place the Province within the map of its Region $mContinents["Europe"][$mCountries[$a2D[$i][0]]][$mRegions[$a2D[$i][5]]][$a2D[$i][2]] = "" ; the last leaf on the branch EndSwitch Next Return $mContinents EndFunc ;==>Example1 ; --- Example 2 --- Func Example2() ; Create a MAP containing a nested MAP containing Arrays Local $mMyMAP[], $mMAP[] $mMyMAP["FirsKey"] = "Simple String" $mMyMAP["SecondKey"] = 150 ; a number $mMyMAP["Calendar"] = $mMAP Local $iYear = @YEAR ; -> Example 2026 $mMyMAP["Calendar"][$iYear] = $mMAP ; Key "Calendar" subKey 2026 contains another empty MAP For $i = 1 To 12 ; Populate the 2026 map with 12 keys each named as a month. $mMyMAP["Calendar"][$iYear][_DateToMonth($i)] = _GenerateMonth($iYear, $i) ; <- Each 2D array contains the calendar for that month. ; _ArrayDisplay($mMyMAP["Calendar"][$iYear][_DateToMonth($i)]) ; Debug Next ; Add some 1D and 2D arrays to the MAP Local $aMonths[12] = [ _ "January", "February", "March", "April", _ "May", "June", "July", "August", _ "September", "October", "November", "December" _ ] $mMyMAP["Months"] = $aMonths Local $aMonthsMultiLang = [ _ ["English", "Italiano", "Español", "Français", "Deutsch", "Português", "Nederlands", "Polski"], _ ["January", "Gennaio", "Enero", "Janvier", "Januar", "Janeiro", "Januari", "Styczeń"], _ ["February", "Febbraio", "Febrero", "Février", "Februar", "Fevereiro", "Februari", "Luty"], _ ["March", "Marzo", "Marzo", "Mars", "März", "Março", "Maart", "Marzec"], _ ["April", "Aprile", "Abril", "Avril", "April", "Abril", "April", "Kwiecień"], _ ["May", "Maggio", "Mayo", "Mai", "Mai", "Maio", "Mei", "Maj"], _ ["June", "Giugno", "Junio", "Juin", "Juni", "Junho", "Juni", "Czerwiec"], _ ["July", "Luglio", "Julio", "Juillet", "Juli", "Julho", "Juli", "Lipiec"], _ ["August", "Agosto", "Agosto", "Août", "August", "Agosto", "Augustus", "Sierpień"], _ ["September", "Settembre", "Septiembre", "Septembre", "September", "Setembro", "September", "Wrzesień"], _ ["October", "Ottobre", "Octubre", "Octobre", "Oktober", "Outubro", "Oktober", "Październik"], _ ["November", "Novembre", "Noviembre", "Novembre", "November", "Novembro", "November", "Listopad"], _ ["December", "Dicembre", "Diciembre", "Décembre", "Dezember", "Dezembro", "December", "Grudzień"] _ ] $mMyMAP["MultiLangMonths"] = $aMonthsMultiLang Local $aZodiacSigns[12] = [ _ "Aries", "Taurus", "Gemini", "Cancer", _ "Leo", "Virgo", "Libra", "Scorpio", _ "Sagittarius", "Capricorn", "Aquarius", "Pisces" _ ] $mMyMAP["ZodiacSigns"] = $aZodiacSigns Local $aFamousScientists[6] = [ _ "Albert Einstein", "Isaac Newton", "Marie Curie", _ "Galileo Galilei", "Nikola Tesla", "Charles Darwin" _ ] $mMyMAP["Scientists"] = $aFamousScientists Local $aFamousPainters[6] = [ _ "Leonardo da Vinci", "Vincent van Gogh", "Pablo Picasso", _ "Claude Monet", "Michelangelo", "Rembrandt" _ ] $mMyMAP["Painters"] = $aFamousPainters Local $aFamousMusicians[6] = [ _ "Wolfgang Amadeus Mozart", "Ludwig van Beethoven", "Johann Sebastian Bach", _ "Frederic Chopin", "Pyotr Ilyich Tchaikovsky", "Jimi Hendrix" _ ] $mMyMAP["Musicians"] = $aFamousMusicians ; ------------------------------------- ; check for VarTypes output on TreeView ; ------------------------------------- Local $aArray[2] = [1, "Example"] Local $mMAP[] Local $dBinary = Binary("0x00204060") Local $bBoolean = False Local $pPtr = Ptr(-1) Local $hWnd = WinGetHandle(AutoItWinGetTitle()) Local $iInt = 1 Local $fFloat = 2.9 Local $oObject = ObjCreate("Scripting.Dictionary") Local $sString = "Some text" Local $tStruct = DllStructCreate("wchar[256]") Local $vKeyword = Default Local $fuFunc = ConsoleWrite Local $fuUserFunc = Example1 ; a 2D array containing also a sub array a MAP and other VarTypes ; nested array, MAP and not "printable" variables are indicated but not expanded Local $aWorldCities = [["London", "New York", "Tokyo", $oObject], _ ["Rome", "Beijing", $aFamousPainters, "Moscow"], _ [MsgBox, "Munich", "Athens", "Lisbon"], _ [$mMAP, "Warsaw", "Prague", "Copenhagen"]] $mMyMAP["NestedArray"] = $aWorldCities $mMyMAP["VarTypes"] = $mMAP $mMyMAP["VarTypes"]["Array"] = $aArray $mMyMAP["VarTypes"]["Map"] = $mMAP $mMyMAP["VarTypes"]["Binary"] = $dBinary $mMyMAP["VarTypes"]["Boolean"] = $bBoolean $mMyMAP["VarTypes"]["Ptr"] = $pPtr $mMyMAP["VarTypes"]["Wnd"] = $hWnd $mMyMAP["VarTypes"]["Int"] = $iInt $mMyMAP["VarTypes"]["Float"] = $fFloat $mMyMAP["VarTypes"]["Object"] = $oObject $mMyMAP["VarTypes"]["String"] = $sString $mMyMAP["VarTypes"]["Struct"] = $tStruct $mMyMAP["VarTypes"]["Keyword"] = $vKeyword $mMyMAP["VarTypes"]["Func"] = MsgBox $mMyMAP["VarTypes"]["Func2"] = $fuFunc $mMyMAP["VarTypes"]["Func3"] = Example1 $mMyMAP["VarTypes"]["UserFunc"] = $fuUserFunc Return $mMyMAP EndFunc ;==>Example2 ; ======================== Func _StringToArray2D($sFile, $delim = ";") ; this function is extracted, adapted and slightly modified from the _FileReadToArray() function in file.au3 Local $aArray ; remove last line separator if any at the end of the file If StringRight($sFile, 1) = @LF Then $sFile = StringTrimRight($sFile, 1) If StringRight($sFile, 1) = @CR Then $sFile = StringTrimRight($sFile, 1) If StringInStr($sFile, @LF) Then $aArray = StringSplit(StringStripCR($sFile), @LF) ElseIf StringInStr($sFile, @CR) Then ;; @LF does not exist so split on the @CR $aArray = StringSplit($sFile, @CR) Else ;; unable to split the file If StringLen($sFile) Then Dim $aArray[2] = [1, $sFile] ; returns the whole file in one element Else Return SetError(2, 0, 0) ; File is empty EndIf EndIf ; now split 1D $aArray to 2D $aColumns according to delimiter Local $aColumns[$aArray[0]][1] ; create a new [2D] array with same nr. of lines of $aArray For $i = 1 To $aArray[0] ; scan all lines of the array $TempRow = StringSplit($aArray[$i], $delim, 2) ; split the line If UBound($TempRow) > UBound($aColumns, 2) Then ReDim $aColumns[$aArray[0]][UBound($TempRow)] For $ii = 0 To UBound($TempRow) - 1 $aColumns[$i - 1][$ii] = $TempRow[$ii] Next Next Return $aColumns EndFunc ;==>_StringToArray2D ;============================================== Func _GenerateMonth($iYear = @YEAR, $iMonth = @MON, $ISO = True) ; this function creates a month calendar into an 7x7 array Local $aMonth[7][7], $iDOW Local $iFirstDOW = $ISO ? _DateToDayOfWeekISO($iYear, $iMonth, 1) : _DateToDayOfWeek($iYear, $iMonth, 1) For $iDay = 1 To 7 $aMonth[0][$iDay - 1] = $ISO ? _DateDayOfWeek(Mod($iDay, 7) + $ISO, $DMW_LOCALE_SHORTNAME) : _DateDayOfWeek($iDay, $DMW_LOCALE_SHORTNAME) Next For $iDay = 1 To _DateDaysInMonth($iYear, $iMonth) $iDOW = $ISO ? _DateToDayOfWeekISO($iYear, $iMonth, $iDay) : _DateToDayOfWeek($iYear, $iMonth, $iDay) $aMonth[Int(($iFirstDOW + $iDay - 2) / 7) + 1][$iDOW - 1] = $iDay Next Return $aMonth EndFunc ;==>_GenerateMonth Edited 5 hours ago by Gianni mLipok, WildByDesign, donnyh13 and 2 others 5 Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....
WildByDesign Posted 2 hours ago Posted 2 hours ago This visual representation of maps and nested maps in a TreeView is very beneficial. Thank you. I've always had difficultly trying to "visualize" map and nested map structure in my mind. So this is really helpful to visualize everything. Excellent work and thank you for sharing. donnyh13 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now