jeeswg's Scripting.Dictionary mini-tutorial

Put simple Tips and Tricks that are not entire Tutorials in this forum
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

jeeswg's Scripting.Dictionary mini-tutorial

31 Jul 2019, 10:06

==================================================

jeeswg's Scripting.Dictionary mini-tutorial

==================================================

PROPERTY SYNTAX

A note: re. obj.property[] v. obj.property() syntax:

Code: Select all

oDict.Item[vKey] := vValue ;AHK v1/v2
oDict.Item(vKey) := vValue ;AHK v1 only
==================================================

LINKS

Dictionary object | Microsoft Docs
https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dictionary-object

COM Object Reference [AutoHotkey v1.1+] - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/56987-com-object-reference-autohotkey-v11/#entry357748
COM Object Reference - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=77&p=396#p396

[using old COM syntax]
Scripting.Dictionary Object as Associative Array - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/16394-scriptingdictionary-object-as-associative-array/

jeeswg's objects tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=29232
Scripting.Dictionary objects are used in 3 of the following 5 chapters (marked '>'):
FREQUENCY COUNT (CASE INSENSITIVE) (SORT)
> FREQUENCY COUNT (CASE INSENSITIVE) (MAINTAIN ORDER)
> FREQUENCY COUNT (CASE SENSITIVE) (MAINTAIN ORDER/SORT)
REMOVE DUPLICATES (CASE INSENSITIVE) (MAINTAIN ORDER/SORT)
> REMOVE DUPLICATES (CASE SENSITIVE) (MAINTAIN ORDER/SORT)

==================================================

EXAMPLES

Some examples.

Scripting.Dictionary v. AHK basic object:
2 key differences:
Scripting.Dictionary: case-sensitive keys (or case-insensitive/locale), creation order
AHK basic object: case-insensitive keys, alphabetical order

Code: Select all

;q:: ;Scripting.Dictionary examples
oDict := ComObjCreate("Scripting.Dictionary")

;set compare mode:
;oDict.CompareMode := 0 ;case sensitive ;vbBinaryCompare := 0
;oDict.CompareMode := 1 ;case insensitive ;vbTextCompare := 1
;MsgBox, % oDict.CompareMode

;note: you get an error if you try to change compare mode after adding keys to the dictionary
;oDict.CompareMode := 1
;MsgBox, % oDict.CompareMode
;oDict.Add("key1", "value1")
;oDict.CompareMode := 0
;MsgBox, % oDict.CompareMode

;set item:
;note: key names are case sensitive
;note: error if try to add the same item twice
oDict.Add("key1", "value1")
oDict.Add("Key1", "Value1")

;set item (alternative):
;note: key names are case sensitive
;oDict.Item("key2") := "value2" ;valid in AHK v1, but not AHK v2
oDict.Item["key2"] := "value2"
oDict.Item["Key2"] := "Value2"

;MsgBox, % oDict.Count
;oDict.Item["key3"] ;this creates a key, even though no value is specified
;MsgBox, % oDict.Count

;get item:
;note: key names are case sensitive
MsgBox, % oDict.Item["key1"] ;value1
MsgBox, % oDict.Item["Key1"] ;Value1

;list keys (creation order, not alphabetical order):
vOutput := ""
for vKey in oDict
	vOutput .= vKey " " oDict.Item[vKey] "`r`n"
MsgBox, % vOutput

;list keys (alternative):
vOutput := ""
oKeys := oDict.Keys
for vKey in oKeys
	vOutput .= vKey " " oDict.Item[vKey] "`r`n"
MsgBox, % vOutput

;list items:
vOutput := ""
oItems := oDict.Items
for vItem in oItems
	vOutput .= vItem "`r`n"
MsgBox, % vOutput

;get item count:
MsgBox, % oDict.Count ;4

;check if key exists:
MsgBox, % oDict.Exists("key1") ;-1 (true)
MsgBox, % oDict.Exists("key3") ;0 (false)

;delete key:
MsgBox, % oDict.Exists("key1") " " oDict.Count ;-1 4
oDict.Remove("key1")
MsgBox, % oDict.Exists("key1") " " oDict.Count ;0 3

;rename key (and preserve its creation order):
MsgBox, % oDict.Exists("key2") " " oDict.Exists("key3") ;-1 0
;oDict.Key(vKeyOld) := vKeyNew
oDict.Key["key2"] := "key3" ;error if new key name already exists
MsgBox, % oDict.Exists("key2") " " oDict.Exists("key3") ;0 -1

;delete all keys:
oDict.RemoveAll()
MsgBox, % oDict.Count ;0

;delete object:
oDict := ""
return

Code: Select all

;w:: ;AHK basic object examples
oArray := {}

;set item:
;note: key names are case insensitive
;the second assignment overwrites the first
oArray["key1"] := "value1"
oArray["Key1"] := "Value1" ;assigned to key1
oArray["key2"] := "value2"
oArray["Key2"] := "Value2" ;assigned to key2

;get item:
;note: key names are case sensitive
MsgBox, % oArray["key1"] ;Value1
MsgBox, % oArray["Key1"] ;Value1 ;retrieves from key1

;list keys (alphabetical order, not creation order):
vOutput := ""
for vKey, vValue in oArray
	vOutput .= vKey " " vValue "`r`n"
MsgBox, % vOutput

;get item count:
MsgBox, % oArray.Count() ;2

;check if key exists:
MsgBox, % oArray.HasKey("key1") ;1 (true)
MsgBox, % oArray.HasKey("key3") ;0 (false)

;delete key:
MsgBox, % oArray.HasKey("key1") " " oArray.Count() ;1 2
oArray.Delete("key1")
MsgBox, % oArray.HasKey("key1") " " oArray.Count() ;0 1

;rename key:
oArray["key3"] := oArray.Delete("key2")

;delete all keys (approximately):
oArray.Delete("", Chr(0xFFFF))
MsgBox, % oArray.Count() ;0

;delete object:
oArray := ""
return
==================================================
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 25 guests