pixelsearch Posted June 13, 2024 Posted June 13, 2024 (edited) Hello everybody I'm a total newbie to Maps in AutoIt. Instead, I used a few times the similar Scripting.Dictionary object (which is damn fast) . Maybe the advantage of Maps could be that, if one day MS removes the Scripting.Dictionary object (I doubt it but who knows...) then we'll be happy that Maps exist in recent versions of AutoIt. Anyway, I just started to learn Maps and needed to display at same time the Keys & their corresponding Values. So I scripted this simple reusable function which seems to do the job and is called by one line : _ArrayDisplay(_Map2D($mMap), "Map Keys & Values") Here is the functional example corresponding to the pic : ; AutoIt 3.3.16.1 (Map) #include <Array.au3> local $mMap[] $mMap["Beethoven"] = "Bee" $mMap["Berlioz"] = "Ber" $mMap["Chopin"] = Null ; create a key "Chopin" without assigning it a value _ArrayDisplay(_Map2D($mMap), "Map Keys & Values") Func _Map2D(Const ByRef $mMap) Local $aMapKeys = MapKeys($mMap) Local $aMap2D[Ubound($aMapKeys)][2] Local $iRow = - 1 For $vKey In $aMapKeys ; or a simple For... loop as commented out below $iRow += 1 ; 0+ $aMap2D[$iRow][0] = $vKey $aMap2D[$iRow][1] = $mMap[$vKey] Next ;~ For $i = 0 To Ubound($aMapKeys) - 1 ;~ $aMap2D[$i][0] = $aMapKeys[$i] ;~ $aMap2D[$i][1] = $mMap[$aMapKeys[$i]] ;~ Next Return $aMap2D EndFunc I just tested it on 10.000 pairs of key/values created by using the code below and the return from the function was immediate : For $i = 0 To 9999 $mMap[$i] = $i + 10 Next If you guys got some improvements for the function, please share them here, thanks. Edited June 13, 2024 by pixelsearch typo donnyh13, Zedna and ioa747 2 1 "I think you are searching a bug where there is no bug... don't listen to bad advice."
ioa747 Posted June 14, 2024 Posted June 14, 2024 you might find this interesting 211135-map-data-fromto-file I know that I know nothing
ioa747 Posted April 17 Posted April 17 (edited) I have made some helpful functions for maps based on yours, which I use regularly and I wanted to share them Thank you very much expandcollapse popup#include <Array.au3> #include <String.au3> Local $mMap[], $mMapOfMap[] $mMap["Beethoven"] = "Bee" $mMap["Berlioz"] = "Ber" $mMap["Chopin"] = Null ; create a key "Chopin" without assigning it a value Local $aArray = [$mMap.Beethoven, $mMap.Berlioz, $mMap.Chopin] $mMap["Array"] = $aArray $mMapOfMap.Map1 = $mMap ConsoleWrite("$mMap.Array[1]=" & $mMap.Array[1] & @CRLF) _MapCW($mMap) _MapClear($mMap) _MapCW($mMap) _MapCW($mMapOfMap) _MapCW($mMapOfMap.Map1) ConsoleWrite("" & @CRLF) ConsoleWrite("$mMapOfMap.Map1.Array[1]=" & $mMapOfMap.Map1.Array[1] & @CRLF) ConsoleWrite("$mMapOfMap.Map1.Berlioz=" & $mMapOfMap.Map1.Berlioz & @CRLF) Func _Map2D(ByRef $mMap) Local $aMapKeys = MapKeys($mMap) Local $aMap2D[UBound($aMapKeys)][2] For $i = 0 To UBound($aMapKeys) - 1 $aMap2D[$i][0] = $aMapKeys[$i] $aMap2D[$i][1] = $mMap[$aMapKeys[$i]] Next Return $aMap2D EndFunc ;==>_Map2D Func _MapCW(ByRef $m, $sTitle = "--- Map info ---") ConsoleWrite($sTitle & @CRLF) Local $aObject = _Map2D($m) Local $iMaxKeyLen = 0 Local $sKey For $i = 0 To UBound($aObject) - 1 $sKey = $aObject[$i][0] If StringLen($sKey) > $iMaxKeyLen Then $iMaxKeyLen = StringLen($sKey) EndIf Next For $i = 0 To UBound($aObject) - 1 $sKey = $aObject[$i][0] Local $vValue = $aObject[$i][1] Local $sPadding = _StringRepeat(" ", $iMaxKeyLen - StringLen($sKey) + 1) Local $sDim, $sLabel = "" Local $iDimension If IsMap($vValue) Then $sLabel = "= {Map[" & UBound($vValue) & "]}" ConsoleWrite($sKey & $sPadding & $sLabel & @CRLF) ElseIf IsArray($vValue) Then $iDimension = UBound($vValue, $UBOUND_DIMENSIONS) ; The dimension of the array e.g. 1/2/3 dimensional. $sDim = "" For $d = 1 To $iDimension $sDim &= "[" & UBound($vValue, $d) & "]" Next $sLabel = "= {Array" & $sDim & "}" ConsoleWrite($sKey & $sPadding & $sLabel & @CRLF) Else ConsoleWrite($sKey & $sPadding & "= " & $vValue & @CRLF) EndIf Next EndFunc ;==>_MapCW Func _MapClear(ByRef $mMap) Local $m[] $mMap = $m EndFunc ;==>_MapClear Edited April 17 by ioa747 argumentum and pixelsearch 2 I know that I know nothing
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