Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Outer and inner loop with A_Index


  • Please log in to reply
2 replies to this topic
briter
  • Members
  • 148 posts
  • Last active: Jan 31 2018 05:41 PM
  • Joined: 13 Nov 2014

Hi Community,

 

i just need to know if I can use A_Index in an inner and outer loop at the same time:

 

Loop, 5

{

    MsgBox %A_Index%

    Loop, 2

    {

         MsgBox %A_Index%

    }

}

 

Hopefully this will result in MsgBox showing 1, 1, 2,    2, 1, 2,    3, 1, 2,    4, 1,2,    5, 1, 2  ?

 

EDIT:

 

I realized I can get the answer by running the code, when I pressed the post button ;-)

 

Problem solved!



G. Sperotto
  • Members
  • 539 posts
  • Last active: Jun 20 2015 04:54 PM
  • Joined: 12 Dec 2011

Hi Briter,

Welcome to the AutoHotkey community forums.

 

I see you already found the answer to your question. But just in case, here is some more info on using A_Index for recursive loops:

 

If used inside an inner loop, A_Index refers to the inner loop, if used inside an outer loop AND outside an inner loop, A_Index refers to the outer loop. You can also save the value of the outer loop for usage inside the inner loop by assigning A_Index to another variable before entering the inner loop. This is specially usefull when you wish to loop through a two dimensional object (such as a table).

 

Example usage (code below loops through all the values of ABC):

ABC := object()

ABC[1] := [1,2,3]
ABC[2] := [4,5,6]

Loop % ABC.MaxIndex() ; One iteration for each of the two rows.
{
CURRENT_ROW := A_Index ; This is how we save the values of the current iteration of the outer loop for use inside the inner loop.
Loop % ABC[1].MaxIndex() ; One iteration for each of the three columns.
{
msgbox % ABC[CURRENT_ROW, A_Index] ; Example: if we are at the second iteration of the outer loop and first of the inside loop, this call will dereference ABC[2,1], which is 4.
}
}

"What is a suitable automation? Whatever saves your day for the greater matters."
Barcoder - Create QR Codes and other Barcodes using only Autohotkey !!


briter
  • Members
  • 148 posts
  • Last active: Jan 31 2018 05:41 PM
  • Joined: 13 Nov 2014

Many thanks for your support. 

 

I am going to have to work with arrays in this context soon, so your tip is quite helpful for me.

 

Best Regards