Jump to content
Sign in to follow this  
Kip

Associative Array UDF

Recommended Posts

Kip

An UDF for creating and manipulating associative arrays.

I know there were some examples posted before, but they were all incomplete.

Functions:

$kArray = _Key()                                    ; Create a new associative array
      
      _Key_AddItem(ByRef $kArray, $vKey, $vValue)           ; Add an item to the end
      _Key_DelItem(ByRef $kArray, $vKey)                    ; Delete am item
      _Key_Rename(ByRef $kArray, $vKey, $vNewKey)           ; Set a new name for a key
      
      _Key_GetKeys(ByRef $kArray)                           ; Get a normal array with all the keys
      _Key_GetValues(ByRef $kArray)                     ; Get a normal array with all the values
      _Key_Sort(ByRef $objDict, $iSort=$KEY_Key, $iOrder=$KEY_Asc) ; Sort an array by key or value and ascending or descending
      
      _Key_KeyExists(ByRef $kArray, $vKey)              ; Check whether an item exists
      _Key_GetSize(ByRef $kArray)                           ; Get the number of items
      
      _Key_Empty(ByRef $kArray)                         ; Delete all items from the array
      
      _Key_FromArray(ByRef $aArray)                     ; Create an associative array from a regular one
      _Key_ToArray(ByRef $kArray)                           ; Create a normal array from an associative array
      
      _Key_Print(ByRef $kArray, $iReturn=0, $sTitle="Array"); Print the keys to the output stream, or return it as a string

It's very similar to PHP.

Example:

#include <Array.au3>
   #include <Key.au3>
   
   $kArray = _Key()
   
   $kArray("key1") = "val1"
   $kArray("key2") = "val2"
   
   
   
   MsgBox(0,"Example", $kArray("key2") )
   
   _Key_DelItem($kArray, "key2")
   
   MsgBox(0,"Deleted", $kArray("key2") )
   
   
   
   _Key_Empty($kArray)
   
   
   $kArray(0) = "val1"
   $kArray("1") = "val2"
   
   $aArray = _Key_ToArray($kArray)
   
   _ArrayDisplay($aArray)

Key.au3

Edited by Kip

Share this post


Link to post
Share on other sites
Xand3r

nice job but imo Scripting.Dictionary is very easy to use straight forward..

$obj.add(key,val)

$obj.item(key)=val

$obj.keys()

$obj.items()

$obj.remove(key)

$obj.removeall()

$obj.exists(key)

^_^

p.s: no way near php ;)

php uses true associative arrays :multi dimensional etc

Edited by TheMadman

Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

Share this post


Link to post
Share on other sites
DaleHohm

Please examine aGorilla's Hash.au3 for comparison. I use it very productively in several projects. http://www.autoitscript.com/forum/index.ph...amp;hl=hash.au3

Dale


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites
Kip

Added 4 new functions. I personally like the sort function very much ^_^

_Key_Rename(ByRef $kArray, $vKey, $vNewKey)
_Key_Sort(ByRef $objDict, $iSort=$KEY_Key, $iOrder=$KEY_Asc)
_Key_GetSize(ByRef $kArray)
_Key_Print(ByRef $kArray, $iReturn=0, $sTitle="Array")
Edited by Kip

Share this post


Link to post
Share on other sites
Kip

p.s: no way near php smile.gif

php uses true associative arrays :multi dimensional etc

Btw, just because it's associative doesn't mean it has to be multi dimensional. ^_^

Share this post


Link to post
Share on other sites
MilesAhead

Added 4 new functions. I personally like the sort function very much :)

_Key_Rename(ByRef $kArray, $vKey, $vNewKey)
_Key_Sort(ByRef $objDict, $iSort=$KEY_Key, $iOrder=$KEY_Asc)
_Key_GetSize(ByRef $kArray)
_Key_Print(ByRef $kArray, $iReturn=0, $sTitle="Array")
I've been looking for an AssocArray to use with AutoIt3. Please answer a question for me though.

I used Key.au3 and I don't understand why this works:

Local $assocMod = _Key()

$assocMod("Alt") = "!"
    $assocMod("Control") = "^"
    $assocMod("Shift") = "+"
    $assocMod("WinKey") = "#"

The lookup by key has to return a reference to an item but I don't see how it does it??

edit: nevermind. Guess it's the default activex property. That makes it nice. :party:

Edited by MilesAhead

Share this post


Link to post
Share on other sites
MilesAhead

nice job but imo Scripting.Dictionary is very easy to use straight forward..

...

I agree. For my purposes I just made a couple of convenience functions in my general purpose include script.

;use Scripting.Dictionary object for simple associative arrays
Func _AssocArray()
    Local $aArray = ObjCreate("Scripting.Dictionary")
    If @error Then
        Return SetError(1, 0, 0)
    EndIf
    $aArray.CompareMode = 1
    Return $aArray
EndFunc   ;==>_AssocArray

Func _AssocArrayDestroy(ByRef $aArray)
    If Not IsObj($aArray) Then
        Return False
    EndIf
    $aArray.RemoveAll()
    $aArray = 0
    Return True
EndFunc   ;==>_AssocArrayDestroy

For everything else I just use the Dictionary methods. :)

All I really want is assignment and lookup by key anyway.

edit: btw glad I found this discussion. The AssocArray is much neater than the alternatives of 2 dimensional arrays or giant Select statements. :party:

Edited by MilesAhead

Share this post


Link to post
Share on other sites
MilesAhead

I agree. [...]It is, but I don't like .member syntax. Or whatever it's called.

Don't get me wrong. Anything that makes something easy to use I'm all for it. Nothing wrong with writing wrapper code. I have a couple of ActiveX Controls that wrap a Windows Control so you can drop it on a form in a different programming language. One is a single instance control with command tail transfer written in Delphi. I can use it in VB, C# VC++ and whatnot.

One thing I did notice, and it's not your code, I think it's .NET. I used the associative array just for lookup of hotkey strings. On my program when it starts up it reads an .ini file and gets the modifier and main key and calls the function to put the 2 pieces together to make the hotkey. Seemed great but starting with Windows it really bogged down. Must have to start up a bunch of .NET stuff to use the Dictionary object I'm guessing.

For small hotkey utilities that sit in the tray I guess it's overkill. Too bad because assoc arrays sure are convenient. I changed the code so that the main hotkey part included the {} brackets. No lookup for that part. Now I just look up the modifier key. It's not obvious to the user what "+" is, but if they see the word Shift in the combo box they know. And they can figure out that {PgUp} is Page Up I'm sure.

Share this post


Link to post
Share on other sites
MilesAhead

Hmmmmmmm it looks like that symptom may have had nothing at all to do with Scripting.Dictionary. Just made that change and assumed it was the cause of slow start up. The culprit I think was trying to validate paths on network drives before the drive letter was assigned. It hung there waiting for the network apparently.

So I take back my knock on Scripting.Dictionary unless I find something else really wrong with it. :)

Edited by MilesAhead

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.