0

I've been thinking - what is the best way to handle loops in ahk script?

For example instead of:

; Collect results 1
Sleep 1000
Addr1 := IE.document.getElementsByClassName("name")[0].innertext
String_Object := StrSplit(addr1, "`,")
If (Substr(Addr1, 1, 2) = "MK")
{
Addr1 := String_Object[2] . "," . Trim(String_Object[3]) . "," . PostCode
MsgBox, %Addr1%
}
Else
{
Addr1 := String_Object[1] . "," . Trim(String_Object[2]) . "," . PostCode
MsgBox, %Addr1%
}


; Collect results 2
Sleep 1000
Addr2 := IE.document.getElementsByClassName("name")[1].innertext
String_Object := StrSplit(addr2, "`,")
If (Substr(Addr2, 1, 2) = "MK")
{
Addr2 := String_Object[2] . "," . Trim(String_Object[3]) . "," . PostCode
MsgBox, %Addr2%
}
Else
{
Addr2 := String_Object[1] . "," . Trim(String_Object[2]) . "," . PostCode
MsgBox, %Addr2%
}

I'd like to do something like this (note this is pseudocode):

j = 0
i = 1
while (i <= 5)
{
Sleep 1000
Addr[i] := IE.document.getElementsByClassName("name")[j].innertext
String_Object := StrSplit(addr[i], "`,")
If (Substr(Addr[i], 1, 2) = "MK")
{
Addr[i] := String_Object[2] . "," . Trim(String_Object[3]) . "," . PostCode
MsgBox, %Addr[i]%
}
Else
{
Addr[i] := String_Object[1] . "," . Trim(String_Object[2]) . "," . PostCode
MsgBox, %Addr[i]%
}

j = j+1
i = i+1
}

Is it possible to accomplish this in AHK?

1
  • 2
    Please elaborate (in words) on what you're trying to achieve. That pseudo-code isn't really telling me anything. Btw: You should really work on your variable naming style. Commented Jul 28, 2014 at 8:45

1 Answer 1

5

I think what you're looking for are Loops and A_Index.

http://www.autohotkey.com/docs/commands/Loop.htm

http://www.autohotkey.com/docs/Variables.htm#Index

A_Index automatically tracks the current loop iteration of the loop in which it exists; it is unique to its loop, and nested loops will track their own A_Index. For an easy example, try the following code:

Loop, 3
{
    MsgBox, Outer-loop %A_Index%
    Loop, 3
    {
        MsgBox, Inner-loop %A_Index%
    }
}

Based on your pseudocode, you would something to the effect of the following:

Loop, 5
{
    Sleep 1000
    Addr[A_Index] := IE.document.getElementsByClassName("name")[A_Index - 1].innertext
    String_Object := StrSplit(addr[A_Index], "`,")
    If (Substr(Addr[A_Index], 1, 2) = "MK")
    {
        Addr[A_Index] := String_Object[2] . "," . Trim(String_Object[3]) . "," . PostCode
        MsgBox, %Addr[A_Index]%
    }
    Else
    {
        Addr[A_Index] := String_Object[1] . "," . Trim(String_Object[2]) . "," . PostCode
        MsgBox, %Addr[A_Index]%
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I did not know about the A_Index automatic variable, the docsare impenetrable. +1 sir!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.