Jump to content

Clean way to declare arrays with letters...


 Share

Recommended Posts

I have done a little searching and want to confirm the following.

OK, I have a script that I want to press particular keys for a particular time. I am trying to clean up my code. ATM I have something like this:

If  [condition] then
        AutoItSetOption ("SendKeyDownDelay", 1000)
        ControlSend ( $handle,"","", "a")
    EndIf

    If  [condition] then
        AutoItSetOption ("SendKeyDownDelay", 10000)
        ControlSend ( $handle,"","", "t")
    EndIf

    If  [condition] then
        AutoItSetOption ("SendKeyDownDelay", 5000)
        ControlSend ( $handle,"","", "y")
    EndIf

    If  [condition] then
        AutoItSetOption ("SendKeyDownDelay", 7000)
        ControlSend ( $handle,"","", "w")
    EndIf

I noticed I have alot of repettitve code, soooo, I want to cut it down to one loop using an array to change the keys and delay. This is something like I have come up with...

Dim $step = 0,
Dim $TA[0] = 1000
Dim $KA[0] = "y"
Dim $TA[1] = 2000
Dim $KA[1] = "d"
Dim $TA[1] = 2000
Dim $KA[2] = "n"
Dim $TA[1] = 2000
Dim $KA[2] = "{space}"; Used as a pause... for now [Looked into "Null" and "Empty"

Do
    $Atest = PixelGetColor ($Ax, $Ay)
    If $Acolor = $Acolortest then
        AutoItSetOption ("SendKeyDownDelay", $TA[$step] + Random (0, 100))
        ControlSend ( $handle,"","", $KA[$step])
        $step = $step + 1
    EndIf
Until $num = 100

Three issues I have with this...

1. Declaring the array one at a time looks messy and makes the code longer.

2. Since I am trying to send a key from the array to "ControlSend" do I declare the array using quotes still? For EX.

$Array[0]= "A"
OR
$Array[0] = A
? I am pretty sure it is
$Array = "A"

3. At some point in the middle of the process I do not want the loop to press a button... SO can I use something like Null to create a pause with the loop? EX:

$array[0] = "A", $array[1] = Null

What I think i can do is declare the Array like this:

Dim $Array[3] = "a","g","r"

So the result will be:

$Array[0] = "a"
$Array[1] = "g"
$Array[2] = "r"

Is this right? Also any suggestions on Questions 2 & 3?

Edited by dakishman

I love AutoIT! It doesn't talk back, it doesn't complain that ur on the computer, it loves to be ran over and over and over... but IT STILL DOESN'T DO THE DISHES... Gatta keep da pimp hand strong...

Link to comment
Share on other sites

Why use the strringsplit command as follows

$TA = StringSplit("1000,2000,2000,2000",",")

$KA = StringSplit("y,d,n,{space}",",")

for $i = 1 To $KA[0]

MsgBox(0, "","$TA[" & $i & "]=" & $TA[$i] & " $KA[" & $i & "]=" & $KA[$i])

Next

That isn't at all what im looking for. I want to stick to arrays. They are easier to manage.

I love AutoIT! It doesn't talk back, it doesn't complain that ur on the computer, it loves to be ran over and over and over... but IT STILL DOESN'T DO THE DISHES... Gatta keep da pimp hand strong...

Link to comment
Share on other sites

According to the documentation for Dim/Global/Local, you can set an initializer for an array.

To initialize an array, specify the values for each element inside square brackets, separated by commas. For multiple dimensions, nest the initializers. You can specify fewer elements in the initializer than declared, but not more. Function calls can also be placed in the initializers of an array. If the function call returns an array, then the one array element will contain that returned array.

Dim $Array1[12]=[3, 7.5, "string"], $array[5] = [8, 4, 5, 9, 1]

Dim $Grid[2][4]=[["Paul", "Jim", "Richard", "Louis"], [485.44, 160.68, 275.16, 320.00]]

This is a pure array solution that is significantly faster than StringSplit and can define multi-dimension arrays. Compare this to the docs for StringSplit. StringSplit can only define a one-dimensional array and puts the number of elements allocated into element 0. This method does not. Also, internally this method uses a lot less memory than StringSplit.

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

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...