Jump to content

Maps : display keys/values in 2D array


Recommended Posts

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")

Mapkeysandvalues.png.5b835f59de18a100664009a29e74bec2.png

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 by pixelsearch
typo
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...