Jump to content

Recommended Posts

Posted (edited)

If you don't like dealing with Scripting.dictionary you might want to try out this thing I whipped up yesterday evening and this morning.

Basically just a simple wrapper around Scripting.dictionary to make the API a little bit less stupid.  By "stupid" I mean "not like the awesome Python dict API".

Created for the following reasons..

  • Wanted to get a basic understanding of AutoItObject.
  • Wanted to get a basic understanding of the Micro unit test framework.
  • As of 11/4/2014 AutoIt Stable has no native dict or associative array type.  A >Map type is in the current AutoIt beta.
  • It's laborious to use Scripting.dictionary all the time.
  • The Python dict API is better than Scripting.dictionary's

I thought about writing my own hash table implementation in AutoIt, but on balance I decided it wasn't worth the effort and just stuck to Scripting.dictionary to save time/effort.

As you can see I've got a decent number of unit tests for such a simple implementation, so it should be quite robust.

#include <Dict2.au3>
#include <Array.au3>

$dict = _DictCreate()
ConsoleWrite($dict.len()) ; Outputs 0

$dict.set("key1", "value1")
$dict.set("key2", "value2")
$dict.set("key3", "value3")
$dict.set("key4", 1)

ConsoleWrite($dict.get("key2"))         ; Outputs 'value2'
ConsoleWrite($dict.len())               ; Outputs 3
ConsoleWrite($dict.contains("key2")     ; Outputs True

$dict.set("key4", $dict.get("key4") + 1)
ConsoleWrite($dict.get("key4"))         ; Outputs 2

$dict.del("key4")
ConsoleWrite($dict.contains("key4"))    ; Outputs False

$aPairs = $dict.pairs()
_ArrayDisplay($aPairs)                  ; Displays 2d array with column one contains keys, and column two 
                                        ; containing associated values

$aKeys = $dict.keys()
_ArrayDisplay($aKeys)                   ; Displays array containing all keys

$aValues = $dict.values()
_ArrayDisplay($aValues)                 ; Displays array of all values

$aDesiredKeys = ["key1", "key3"]
$aValues = $dict.values($aDesiredKeys)
_ArrayDisplay($aValues)                 ; Displays array of values for key1 and key3

GET IT HERE

Edited by therms
  • Moderators
Posted

therms,

 

  Quote

AutoIt has no native dict or associative array type

The experimental Map datatype is pretty much that - although its capabilities extend much further. ;)

Not wishing to denigrate your work at all, just correcting that statement for anyone looking in. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted (edited)

  On 11/4/2014 at 7:39 PM, Melba23 said:

therms,

 

The experimental Map datatype is pretty much that - although its capabilities extend much further. ;)

Not wishing to denigrate your work at all, just correcting that statement for anyone looking in. :)

M23

Nice!  Thanks for pointing that out.  Updated OP.

Edited by therms

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
×
×
  • Create New...