Jump to content

Is this possible: String-based array keys...


Recommended Posts

Some background:

I found DtTvB's AutoIt Web Server and am tinkering with adding some additional functionality to it. One thing that I'm trying to do is parse the QUERY_STRING into an array that can be called to return values. I'd like to create an array with the key=value pairs ('?foo=bar') but be able to call them in the script like this:

$aQuery['foo']

In PHP, it's possible to do something like this: $aQuery['foo'] = 'bar'

Basically, instead of a number, it can use the key value as it's identifier.

I'm not seeing anyway to do this with AutoIT...is there another way to do something like this or do I need to create a function that parses the value out of the query string each time (ick)? I'm using envset() and envget() for now as it sorta mimics the functionality I'm looking for but that just seems like the wrong construct to be using for this. Am I wrong?

TIA

Sean Shrum :: http://www.shrum.net

All my published AU3-based apps and utilities

'Make it idiot-proof, and someone will make a better idiot'

 

Link to comment
Share on other sites

Might look at Scripting Dictionary

Ooh. That seems handy. Is that a memory-only object? Having created a dictionary, can it be efficiently saved as a binary file or something similar, easily reloaded with something like:

$oDictionary_1.Save("C:\Temp\Dict.bin")

$oDictionary_2 = ObjCreate("Scripting.Dictionary")
$oDictionary2.Load("C:\Temp\Dict.bin")

:rolleyes:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Ooh. That seems handy. Is that a memory-only object? Having created a dictionary, can it be efficiently saved as a binary file or something similar, easily reloaded with something like:

$oDictionary_1.Save("C:\Temp\Dict.bin")

$oDictionary_2 = ObjCreate("Scripting.Dictionary")
$oDictionary2.Load("C:\Temp\Dict.bin")

:rolleyes:

http://msdn.microsoft.com/library/default....d67a747c8db.asp

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

I looked over the code and was like 'Whaaaa' :rolleyes:

Actually, this is what I came up with:

func _ValueGet(ByRef $aTemp, $sKey)
    for $i = 1 to $aTemp[0][0]
        if $aTemp[$i][0] = $sKey then return $aTemp[$i][1]
    Next
    return ""
EndFunc

func _KeySet(Byref $aTemp, $sKey, $sValue)
    for $i = 1 to $aTemp[0][0]
        if $aTemp[$i][0] = $sKey then 
            $aTemp[$i][1] = $sValue
            Return
        EndIf
    Next
    $iNext = $aTemp[0][0]+1
    ReDim $aTemp[$iNext+1][2]
    $aTemp[0][0] = $iNext
    $aTemp[$iNext][0] = $sKey
    $aTemp[$iNext][1] = $sValue
    _arraySort($aTemp,0,1,0,2,0)
EndFunc

Haven't had the need to _KeyDelete yet but I'll make that when I need it.

This way I can create multiple arrays in a key=value pairing. Another benefit of this is I can now make keys anyway I want (including spaces):

_KeySet($aMyArray, "Foo 1", "Bar")
_KeySet($aAnotherArray, "Foo 1", "Stool")
$sResult = _ValueGet($aMyArray, "Foo 1")

Thanks for the help.

Sean Shrum :: http://www.shrum.net

All my published AU3-based apps and utilities

'Make it idiot-proof, and someone will make a better idiot'

 

Link to comment
Share on other sites

Hi,

this looks to be developing..

Can you please show a full script working example? - presumably using gafrost's code as an include...?

Best, Randall

No #include is required. Scripting dictionaries are a standard Windows API that you call directly with AutoIt objects:

$oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")    ; Initialize a COM error handler
$oDict = ObjCreate("Scripting.Dictionary")

$sKeys = "Fish,Mammal,Plant,Insect,Virus,Number,Car"
$avKeys = StringSplit($sKeys, ",")
$sItems = "Bass,Horse,Wheat,Ladybug,H5N1,Pi,Edsel"
$avItems = StringSplit($sItems, ",")

For $a = 1 To $avKeys[0]
    $oDict.Add ($avKeys[$a], $avItems[$a])
Next

$oDict.Item ("Number") = 3.14
MsgBox(64, "Results", "Plant = " & $oDict.Item ("Plant") & @CRLF & _
        "Number = " & $oDict.Item ("Number"))

Func MyErrFunc()
    Local $err = $oMyError.number
    If $err = 0 Then $err = -1
    SetError($err)  ; to check for after this function returns
EndFunc   ;==>MyErrFunc

:rolleyes:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...