Associative Arrays: Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
(Created page with "In computer science, an associative array, map, symbol table, or dictionary is an abstract data type composed of a collection of (key, value) pairs, such that each possible ke...")
 
No edit summary
Line 2: Line 2:
An associative array differs from a “normal” array in one major way: rather than being indexed numerically (i.e. 0, 1, 2, 3, …), it is indexed by a key, or a language-like word.  
An associative array differs from a “normal” array in one major way: rather than being indexed numerically (i.e. 0, 1, 2, 3, …), it is indexed by a key, or a language-like word.  
A Scripting.Dictionary is a standard object provided by the Microsoft ScripTing Runtime (scrrun.dll) dependency and is commonly used in VBScript, a reference to which needs to be added in order to create an object in your AutoIt project.
A Scripting.Dictionary is a standard object provided by the Microsoft ScripTing Runtime (scrrun.dll) dependency and is commonly used in VBScript, a reference to which needs to be added in order to create an object in your AutoIt project.
<br />
A dictionary object example by [https://www.autoitscript.com/forum/profile/2709-mhz/ MHz]
<syntaxhighlight lang='autoit'>
Global $vKey, $sItem, $sMsg, $oDictionary
; Create dictionary object
$oDictionary = ObjCreate("Scripting.Dictionary")
If @error Then
    MsgBox(0, '', 'Error creating the dictionary object')
Else
    ; Add keys with items
    $oDictionary.Add ("One",    "Same"  )
    $oDictionary.Add ("Two",    "Car"  )
    $oDictionary.Add ("Three",  "House" )
    $oDictionary.Add ("Four",  "Boat"  )
    If $oDictionary.Exists('One') Then
        ; Display item
        MsgBox(0x0, 'Item One', $oDictionary.Item('One'), 2)
        ; Set an item
        $oDictionary.Item('One') = 'Changed'
        ; Display item
        MsgBox(0x20, 'Did Item One Change?', $oDictionary.Item('One'), 3)
        ; Remove key
        $oDictionary.Remove('One')
        ;
        $oDictionary.Key ('Two') = 'Bike'
    EndIf
    Sleep(1000)
    ; Store items into a variable
    For $vKey In $oDictionary
      $sItem &= $oDictionary.Item($vKey) & @CRLF
    Next
    ; Display items
    MsgBox(0x0, 'Items Count: ' & $oDictionary.Count, $sItem, 3)
    ; Add items into an array
    $aItems = $oDictionary.Items
    ; Display items in the array
    For $i = 0 To $oDictionary.Count -1
        MsgBox(0x0, 'Items [ ' & $i & ' ]', $aItems[$i], 2)
    Next
    Sleep(1000)
    ; Add keys into an array
    $aKeys = $oDictionary.Keys
    ; Display keys in the array
    For $i = 0 To $oDictionary.Count -1
        MsgBox(0x0, 'Keys [ ' & $i & ' ]', $aKeys[$i], 2)
    Next
EndIf
</syntaxhighlight>

Revision as of 10:17, 28 November 2017

In computer science, an associative array, map, symbol table, or dictionary is an abstract data type composed of a collection of (key, value) pairs, such that each possible key appears at most once in the collection. An associative array differs from a “normal” array in one major way: rather than being indexed numerically (i.e. 0, 1, 2, 3, …), it is indexed by a key, or a language-like word. A Scripting.Dictionary is a standard object provided by the Microsoft ScripTing Runtime (scrrun.dll) dependency and is commonly used in VBScript, a reference to which needs to be added in order to create an object in your AutoIt project.
A dictionary object example by MHz

Global $vKey, $sItem, $sMsg, $oDictionary 

; Create dictionary object
$oDictionary = ObjCreate("Scripting.Dictionary")

If @error Then
    MsgBox(0, '', 'Error creating the dictionary object')
Else
    ; Add keys with items
    $oDictionary.Add ("One",    "Same"  )
    $oDictionary.Add ("Two",    "Car"   )
    $oDictionary.Add ("Three",  "House" )
    $oDictionary.Add ("Four",   "Boat"  )

    If $oDictionary.Exists('One') Then
        ; Display item
        MsgBox(0x0, 'Item One', $oDictionary.Item('One'), 2)
        ; Set an item
        $oDictionary.Item('One') = 'Changed'
        ; Display item
        MsgBox(0x20, 'Did Item One Change?', $oDictionary.Item('One'), 3)
        ; Remove key
        $oDictionary.Remove('One')
        ; 
        $oDictionary.Key ('Two') = 'Bike'
    EndIf

    Sleep(1000)

    ; Store items into a variable
    For $vKey In $oDictionary
       $sItem &= $oDictionary.Item($vKey) & @CRLF
    Next

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

    ; Add items into an array
    $aItems = $oDictionary.Items

    ; Display items in the array
    For $i = 0 To $oDictionary.Count -1
        MsgBox(0x0, 'Items [ ' & $i & ' ]', $aItems[$i], 2)
    Next

    Sleep(1000)

    ; Add keys into an array
    $aKeys = $oDictionary.Keys

    ; Display keys in the array
    For $i = 0 To $oDictionary.Count -1
        MsgBox(0x0, 'Keys [ ' & $i & ' ]', $aKeys[$i], 2)
    Next
EndIf