Search code examples
autoit

Propper working with arrays and variables


Good morning all together!

I'm quite new to this language and I got a question about arrays. Normally I'm working with Python, so for me it is really something new, especially with arrays many things I could easily do before are not working anymore.

My problem is: I'm spliting a string and I will get 4 new strings back. Now I could do a Dim[4] and everything would be fine, but the thing is I only need the last two values.

Normally I would make sure that only the last two values of the array are returning from the function and then just put them into my two variables, all in one line. But here in AutoIt I can't figure out a simple way.

Big thanks for all help you can give me about arrays, apparently they are the new villain in my life


Solution

  • That's easy with StringSplit. It even creates the array with the correct dimension for you:

    #include <array.au3>
    $sString = "alpha,beta,gamma,delta"
    $aString = StringSplit($sString, ",")
    _ArrayDisplay($aString, "StringSplit") 
    ConsoleWrite("From StringSplit, third: " & $aString[3] & @CRLF)
    ConsoleWrite("From StringSplit, fourth: " & $aString[4] & @CRLF)
    ; extract the last two elements:
    $aString = _ArrayExtract($aString, UBound($aString) - 2)
    _ArrayDisplay($aString, "Extracted")
    ConsoleWrite("From Extracted, first: " & $aString[0] & @CRLF)
    ConsoleWrite("From Extracted, second: " & $aString[1] & @CRLF)