RosettaCodeData/Task/Associative-array-Creation/AutoIt/associative-array-creation....

128 lines
3.4 KiB
Plaintext

; Associative arrays in AutoIt.
; All the required functions are below the examples.
; Initialize an error handler to deal with any COM errors..
global $oMyError = ObjEvent("AutoIt.Error", "AAError")
; first example, simple.
global $simple
; Initialize your array ...
AAInit($simple)
AAAdd($simple, "Appple", "fruit")
AAAdd($simple, "Dog", "animal")
AAAdd($simple, "Silicon", "tetravalent metalloid semiconductor")
ConsoleWrite("It is well-known that Silicon is a " & AAGetItem($simple, "Silicon") & "." & @CRLF)
ConsoleWrite(@CRLF)
; A more interesting example..
$ini_path = "AA_Test.ini"
; Put this prefs section in your ini file..
; [test]
; foo=foo value
; foo2=foo2 value
; bar=bar value
; bar2=bar2 value
global $associative_array
AAInit($associative_array)
; We are going to convert this 2D array into a cute associative array where we
; can access the values by simply using their respective key names..
$test_array = IniReadSection($ini_path, "test")
for $z = 1 to 2 ; do it twice, to show that the items are *really* there!
for $i = 1 to $test_array[0][0]
$key_name = $test_array[$i][0]
ConsoleWrite("Adding '" & $key_name & "'.." & @CRLF)
; key already exists in "$associative_array", use the pre-determined value..
if AAExists($associative_array, $key_name) then
$this_value = AAGetItem($associative_array, $key_name)
ConsoleWrite("key_name ALREADY EXISTS! : =>" & $key_name & "<=" & @CRLF)
else
$this_value = $test_array[$i][1]
; store left=right value pair in AA
if $this_value then
AAAdd($associative_array, $key_name, $this_value)
endif
endif
next
next
ConsoleWrite(@CRLF & "Array Count: =>" & AACount($associative_array) & "<=" & @CRLF)
AAList($associative_array)
ConsoleWrite(@CRLF & "Removing 'foo'..")
AARemove($associative_array, "foo")
ConsoleWrite(@CRLF & "Array Count: =>" & AACount($associative_array) & "<=" & @CRLF)
AAList($associative_array)
AAWipe($associative_array)
; end
func AAInit(ByRef $dict_obj)
$dict_obj = ObjCreate("Scripting.Dictionary")
endfunc
; Adds a key and item pair to a Dictionary object..
func AAAdd(ByRef $dict_obj, $key, $val)
$dict_obj.Add($key, $val)
If @error Then return SetError(1, 1, -1)
endfunc
; Removes a key and item pair from a Dictionary object..
func AARemove(ByRef $dict_obj, $key)
$dict_obj.Remove($key)
If @error Then return SetError(1, 1, -1)
endfunc
; Returns true if a specified key exists in the associative array, false if not..
func AAExists(ByRef $dict_obj, $key)
return $dict_obj.Exists($key)
endfunc
; Returns a value for a specified key name in the associative array..
func AAGetItem(ByRef $dict_obj, $key)
return $dict_obj.Item($key)
endfunc
; Returns the total number of keys in the array..
func AACount(ByRef $dict_obj)
return $dict_obj.Count
endfunc
; List all the "Key" > "Item" pairs in the array..
func AAList(ByRef $dict_obj)
ConsoleWrite("AAList: =>" & @CRLF)
local $k = $dict_obj.Keys ; Get the keys
; local $a = $dict_obj.Items ; Get the items (for reference)
for $i = 0 to AACount($dict_obj) -1 ; Iterate the array
ConsoleWrite($k[$i] & " ==> " & AAGetItem($dict_obj, $k[$i]) & @CRLF)
next
endfunc
; Wipe the array, obviously.
func AAWipe(ByRef $dict_obj)
$dict_obj.RemoveAll()
endfunc
; Oh oh!
func AAError()
Local $err = $oMyError.number
If $err = 0 Then $err = -1
SetError($err) ; to check for after this function returns
endfunc
;; End AA Functions.