GaryFrost Posted June 4, 2007 Share Posted June 4, 2007 Just something for those that don't know about the Scripting Dictionary expandcollapse popupGlobal $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 Professor_Bernd 1 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 More sharing options...
Fabry Posted August 3, 2007 Share Posted August 3, 2007 Beatiful. A lan chat (Multilanguage)LanMuleFile transferTank gameTank 2 an online game[center]L'esperienza è il nome che tutti danno ai propri errori.Experience is the name everyone gives to their mistakes.Oscar Wilde[/center] Link to comment Share on other sites More sharing options...
ptrex Posted August 3, 2007 Share Posted August 3, 2007 @Lobishomen Nice. But this has been done before. regards, ptrex Contributions :Firewall Log Analyzer for XP - Creating COM objects without a need of DLL's - UPnP support in AU3Crystal Reports Viewer - PDFCreator in AutoIT - Duplicate File FinderSQLite3 Database functionality - USB Monitoring - Reading Excel using SQLRun Au3 as a Windows Service - File Monitor - Embedded Flash PlayerDynamic Functions - Control Panel Applets - Digital Signing Code - Excel Grid In AutoIT - Constants for Special Folders in WindowsRead data from Any Windows Edit Control - SOAP and Web Services in AutoIT - Barcode Printing Using PS - AU3 on LightTD WebserverMS LogParser SQL Engine in AutoIT - ImageMagick Image Processing - Converter @ Dec - Hex - Bin -Email Address Encoder - MSI Editor - SNMP - MIB ProtocolFinancial Functions UDF - Set ACL Permissions - Syntax HighLighter for AU3ADOR.RecordSet approach - Real OCR - HTTP Disk - PDF Reader Personal Worldclock - MS Indexing Engine - Printing ControlsGuiListView - Navigation (break the 4000 Limit barrier) - Registration Free COM DLL Distribution - Update - WinRM SMART Analysis - COM Object Browser - Excel PivotTable Object - VLC Media Player - Windows LogOnOff Gui -Extract Data from Outlook to Word & Excel - Analyze Event ID 4226 - DotNet Compiler Wrapper - Powershell_COM - New Link to comment Share on other sites More sharing options...
Fabry Posted August 3, 2007 Share Posted August 3, 2007 But this has been done before.Where?? Link me, please. A lan chat (Multilanguage)LanMuleFile transferTank gameTank 2 an online game[center]L'esperienza è il nome che tutti danno ai propri errori.Experience is the name everyone gives to their mistakes.Oscar Wilde[/center] Link to comment Share on other sites More sharing options...
GaryFrost Posted August 3, 2007 Author Share Posted August 3, 2007 http://www.autoitscript.com/forum/index.ph...st&p=256109 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 More sharing options...
GaryFrost Posted August 3, 2007 Author Share Posted August 3, 2007 @LobishomenNice. But this has been done before.regards,ptrexGuess you didn't realize who Lobishomen was..... 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 More sharing options...
ptrex Posted August 3, 2007 Share Posted August 3, 2007 @Gafrost Confused !! Where did I pick up the name Lobishomen ? Nevermind, it's too late and too hot. regards, ptrex Contributions :Firewall Log Analyzer for XP - Creating COM objects without a need of DLL's - UPnP support in AU3Crystal Reports Viewer - PDFCreator in AutoIT - Duplicate File FinderSQLite3 Database functionality - USB Monitoring - Reading Excel using SQLRun Au3 as a Windows Service - File Monitor - Embedded Flash PlayerDynamic Functions - Control Panel Applets - Digital Signing Code - Excel Grid In AutoIT - Constants for Special Folders in WindowsRead data from Any Windows Edit Control - SOAP and Web Services in AutoIT - Barcode Printing Using PS - AU3 on LightTD WebserverMS LogParser SQL Engine in AutoIT - ImageMagick Image Processing - Converter @ Dec - Hex - Bin -Email Address Encoder - MSI Editor - SNMP - MIB ProtocolFinancial Functions UDF - Set ACL Permissions - Syntax HighLighter for AU3ADOR.RecordSet approach - Real OCR - HTTP Disk - PDF Reader Personal Worldclock - MS Indexing Engine - Printing ControlsGuiListView - Navigation (break the 4000 Limit barrier) - Registration Free COM DLL Distribution - Update - WinRM SMART Analysis - COM Object Browser - Excel PivotTable Object - VLC Media Player - Windows LogOnOff Gui -Extract Data from Outlook to Word & Excel - Analyze Event ID 4226 - DotNet Compiler Wrapper - Powershell_COM - New Link to comment Share on other sites More sharing options...
JohnBailey Posted October 29, 2007 Share Posted October 29, 2007 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 More sharing options...
odaylton Posted March 2, 2018 Share Posted March 2, 2018 On 03/08/2007 at 10:28 AM, GaryFrost said: http://www.autoitscript.com/forum/index.ph...st&p=256109 This link does not work anymore please refresh Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted March 2, 2018 Moderators Share Posted March 2, 2018 @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 More sharing options...
jugador Posted March 30, 2023 Share Posted March 30, 2023 (edited) ; 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 April 2, 2023 by jugador Link to comment Share on other sites More sharing options...
jugador Posted April 1, 2023 Share Posted April 1, 2023 (edited) Example of _ItemRemove & _ItemDelete expandcollapse popupGlobal $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 April 2, 2023 by jugador Link to comment Share on other sites More sharing options...
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