Jump to content

Scripting Dictionary


GaryFrost
 Share

Recommended Posts

Just something for those that don't know about the Scripting Dictionary

Global $oDictionary, $oMyError

_Main()

Func _Main()
    ; Create dictionary object
    $oDictionary = _InitDictionary()
    $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")    ; Initialize a COM error handler

    Local $vKey, $sItem, $sMsg

    ; Add keys with items
    _DebugPrint('_AddItem("One", "Same")' & @TAB & _AddItem("One", "Same"))
    _DebugPrint('_AddItem("Two", "Car")' & @TAB & _AddItem("Two", "Car"))
    _DebugPrint('_AddItem("Three", "House")' & @TAB & _AddItem("Three", "House"))
    _DebugPrint('_AddItem("Three", "House")' & @TAB & _AddItem("Three", "House"))
    _DebugPrint('_AddItem("Four", "Boat")' & @TAB & _AddItem("Four", "Boat"))

    If _ItemExists('One') Then
        ; Display item
        MsgBox(0x0, 'Item One', _Item('One'), 2)
        ; Set an item
        _ChangeItem('One', 'Changed')
        ; Display item
        MsgBox(0x20, 'Did Item One Change?', _Item('One'), 3)
        ; Remove key
        _ItemRemove('One')
        ;
    EndIf

    ; Store items into a variable
    For $vKey In $oDictionary
        $sItem &= $vKey & " : " & _Item($vKey) & @CRLF
    Next

    ; Display items
    MsgBox(0x0, 'Items Count: ' & _ItemCount(), $sItem, 3)

    ; Add items into an array
    $aArray = _GetItems()

    ; Display items in the array
    For $i = 0 To _ItemCount() - 1
        MsgBox(0x0, 'Array [ ' & $i & ' ]', $aArray[$i], 2)
    Next

    _DebugPrint('_ItemRemove("Two")' & @TAB & _ItemRemove("Two"))
    _DebugPrint('_ItemRemove("Three")' & @TAB & _ItemRemove("Three"))
    _DebugPrint('_ItemRemove("Three")' & @TAB & _ItemRemove("Three"))
    _DebugPrint('_ItemRemove("Four")' & @TAB & _ItemRemove("Four"))

    ; use keys like an array index
    For $x = 1 To 3
        _AddItem($x, "")
    Next
    $sItem = ""
    _ChangeItem(2, "My Custom Item")
    _ChangeItem(1, "This is the 1st item")
    _ChangeItem(3, "This is the last item")
    For $vKey In $oDictionary
        $sItem &= $vKey & " : " & _Item($vKey) & @CRLF
    Next
    ; Display items
    MsgBox(0x0, 'Items Count: ' & _ItemCount(), $sItem, 3)

    $sItem = ""
    
    _ChangeKey(2, "My New Key")
    For $vKey In $oDictionary
        $sItem &= $vKey & " : " & _Item($vKey) & @CRLF
    Next
    ; Display items
    MsgBox(0x0, 'Items Count: ' & _ItemCount(), $sItem, 3)
    
    $oDictionary.RemoveAll()
    MsgBox(0x0, 'Items Count',_ItemCount(), 3)
    

EndFunc   ;==>_Main

Func _InitDictionary()
    Return ObjCreate("Scripting.Dictionary")
EndFunc   ;==>_InitDictionary

; Adds a key and item pair to a Dictionary object.
Func _AddItem($v_key, $v_item)
    $oDictionary.ADD ($v_key, $v_item)
    If @error Then Return SetError(1, 1, -1)
EndFunc   ;==>_AddItem

; Returns true if a specified key exists in the Dictionary object, false if it does not.
Func _ItemExists($v_key)
    Return $oDictionary.Exists ($v_key)
EndFunc   ;==>_ItemExists

; Returns an item for a specified key in a Dictionary object
Func _Item($v_key)
    Return $oDictionary.Item ($v_key)
EndFunc   ;==>_Item

; Sets an item for a specified key in a Dictionary object
Func _ChangeItem($v_key, $v_item)
    $oDictionary.Item ($v_key) = $v_item
EndFunc   ;==>_ChangeItem

; Sets a key in a Dictionary object.
Func _ChangeKey($v_key, $v_newKey)
    $oDictionary.Key ($v_key) = $v_newKey
EndFunc   ;==>_ChangeKey

; Removes a key, item pair from a Dictionary object.
Func _ItemRemove($v_key)
    $oDictionary.Remove ($v_key)
    If @error Then Return SetError(1, 1, -1)
EndFunc   ;==>_ItemRemove

; Returns the number of items in a collection or Dictionary object.
Func _ItemCount()
    Return $oDictionary.Count
EndFunc   ;==>_ItemCount

; Returns an array containing all the items in a Dictionary object
Func _GetItems()
    Return $oDictionary.Items
EndFunc   ;==>_GetItems

; This is my custom defined error handler
Func MyErrFunc()
    Local $err = $oMyError.number
    If $err = 0 Then $err = -1
    SetError($err)  ; to check for after this function returns
EndFunc   ;==>MyErrFunc

Func _DebugPrint($s_Text)
    ConsoleWrite( _
            "!===========================================================" & @LF & _
            "+===========================================================" & @LF & _
            "-->" & $s_Text & @LF & _
            "+===========================================================" & @LF)
EndFunc   ;==>_DebugPrint

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

  • 1 month later...
  • 2 months later...

This looks WAY RAD!! Gary you rock! I'm excited about using this (as you all might be able to tell).

My only deal is that I can't think of a powerful application of this (yet)

So, Question: What is a powerful application of this?

A decision is a powerful thing
Link to comment
Share on other sites

  • 10 years later...
  • Moderators

@odaylton did you try that handy forum search feature? You'll find all sorts of threads on the topic of scripting dictionary.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • 5 years later...
; Removes multiple key, item pair from a Dictionary object.
Func _ItemRemove($v_key)
    ;~ Remove multiple keys     {multiple [key|key|key]}
    If $v_key = '' Then Return SetError(1, 1, -1)
    Local $v_keyList = StringSplit($v_key, '|', 2)                  ;~ use '|' as delimiter
    For $i = 0 To UBound($v_keyList) - 1
        If Not $oDictionary.Exists($v_keyList[$i]) Then ContinueLoop
        $oDictionary.Remove($v_keyList[$i])
    Next
EndFunc

 

; Delete selective key, item pair from a Dictionary object.
Func _ItemDelete($v_key)
    ;~ Delete selective keys    {unique identifier|Prefix|Suffix}
    If $v_key = '' Then Return SetError(1, 1, -1)
    Local $o_KeyPattern = StringSplit($v_key, '|', 2)
    Local $o_Prefix = ((UBound($o_KeyPattern) > 1) ? (($o_KeyPattern[1] <> '') ? $o_KeyPattern[1] : '.*') : '.*')
    Local $o_Suffix = ((UBound($o_KeyPattern) > 2) ? (($o_KeyPattern[2] <> '') ? $o_KeyPattern[2] : '.*') : '.*')
    Local $sRegex = '\b' & $o_Prefix & $o_KeyPattern[0] & $o_Suffix
    For $o_Key In $oDictionary
        If StringRegExp($o_Key, $sRegex, 0) Then $oDictionary.Remove($o_Key)
    Next
EndFunc

Scripting Dictionary handy for avoiding using of Global :)
> while using GUIRegisterMsg
> or Managing Multiple GUI using GUIGetMsg(1) mode

Edited by jugador
Link to comment
Share on other sites

Example of _ItemRemove & _ItemDelete :)

Global $oDictionary, $oMyError

_Main()
Func _Main()
    ; Create dictionary object
    $oDictionary = _InitDictionary()
    $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")    ; Initialize a COM error handler

;~~ Example Remove
    ConsoleWrite('+ Example_A........' & @CRLF)
    Local $vKey, $sItem, $sMsg
    _AddItem("One", "Same")
    _AddItem("Two", "Car")
    _AddItem("Three", "House")
    _AddItem("Four", "Boat")

    ConsoleWrite('> Display after Add' & @CRLF)
    For $vKey In $oDictionary
        ConsoleWrite($vKey & " : " & _Item($vKey) & @CRLF)
    Next

    _ItemRemove('One|Four|Three')       ;~ Remove multiple keys
    ConsoleWrite('> Display after Remove' & @CRLF)
    For $vKey In $oDictionary
        ConsoleWrite($vKey & " : " & _Item($vKey) & @CRLF)
    Next

    _ItemRemove('Two')                  ;~ Remove single keys


;~~ Example Delete
    ConsoleWrite(@CRLF)
    ConsoleWrite('+ Example_B........' & @CRLF)
    _AddItem("Data_00_Name", "A1")
    _AddItem("Data_00_Address","A2")
    _AddItem("Data_00_Phone", "A3")
    _AddItem("Data_00_Age", "A4")

    _AddItem("Data_01_Name", "B1")
    _AddItem("Data_01_Address","B2")
    _AddItem("Data_01_Phone", "B3")
    _AddItem("Data_01_Age", "B4")

    _AddItem("Data_02_Name", "C1")
    _AddItem("Data_02_Address","C2")
    _AddItem("Data_02_Phone", "C3")
    _AddItem("Data_02_Age", "C4")

    ConsoleWrite('> Display after Add' & @CRLF)
    For $vKey In $oDictionary
        ConsoleWrite($vKey & " : " & _Item($vKey) & @CRLF)
    Next

    _ItemDelete('_01_||')                       ;~ Delete all key contain _01_
    ConsoleWrite('> Display after Delete' & @CRLF)
    For $vKey In $oDictionary
        ConsoleWrite($vKey & " : " & _Item($vKey) & @CRLF)
    Next

    If IsObj($oDictionary) Then $oDictionary = 0
EndFunc   ;==>_Main

Func _InitDictionary()
    Return ObjCreate("Scripting.Dictionary")
EndFunc   ;==>_InitDictionary

; Adds a key and item pair to a Dictionary object.
Func _AddItem($v_key, $v_item)
    $oDictionary.ADD ($v_key, $v_item)
    If @error Then Return SetError(1, 1, -1)
EndFunc   ;==>_AddItem

; Returns an item for a specified key in a Dictionary object
Func _Item($v_key)
    Return $oDictionary.Item ($v_key)
EndFunc   ;==>_Item

; Removes multiple key, item pair from a Dictionary object.
Func _ItemRemove($v_key)
    ;~ Remove multiple keys     {multiple [key|key|key]}
    If $v_key = '' Then Return SetError(1, 1, -1)
    Local $v_keyList = StringSplit($v_key, '|', 2)                  ;~ use '|' as delimiter
    For $i = 0 To UBound($v_keyList) - 1
        If Not $oDictionary.Exists($v_keyList[$i]) Then ContinueLoop
        $oDictionary.Remove($v_keyList[$i])
    Next
EndFunc

; Delete selective key, item pair from a Dictionary object.
Func _ItemDelete($v_key)
    ;~ Delete selective keys    {unique identifier|Prefix|Suffix}
    If $v_key = '' Then Return SetError(1, 1, -1)
    Local $o_KeyPattern = StringSplit($v_key, '|', 2)
    Local $o_Prefix = ((UBound($o_KeyPattern) > 1) ? (($o_KeyPattern[1] <> '') ? $o_KeyPattern[1] : '.*') : '.*')
    Local $o_Suffix = ((UBound($o_KeyPattern) > 2) ? (($o_KeyPattern[2] <> '') ? $o_KeyPattern[2] : '.*') : '.*')
    Local $sRegex = '\b' & $o_Prefix & $o_KeyPattern[0] & $o_Suffix
    For $o_Key In $oDictionary
        If StringRegExp($o_Key, $sRegex, 0) Then $oDictionary.Remove($o_Key)
    Next
EndFunc

; This is my custom defined error handler
Func MyErrFunc()
    Local $err = $oMyError.number
    If $err = 0 Then $err = -1
    SetError($err)  ; to check for after this function returns
EndFunc   ;==>MyErrFunc

 

Edited by jugador
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...