Insert a list of values into a looping script Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Insert a list of values into a looping script

25 Jul 2017, 17:48

Hello,

What is the best way to insert a list of values for a variable in a looping script?

For example, suppose you have a list of 30 numbers:

# | Number
1 | 9
2 | 2
3 | 1
4 | 3
5 | 6
6 | 6
7 | 3
...
30 | 1

And suppose you want insert each of them in order in place of the variable Var in the following script:

Code: Select all

F1::
Var := ??
Loop, 30
{
Step 1
Send Var
Step 2
Step 3
}
Return
I am familiar with the built-in variable A_Index but how would you use that to insert the list of numbers above?

Thanks
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Insert a list of values into a looping script

25 Jul 2017, 18:02

Code: Select all

List := [9,2,1,3,6,6,3]

for key, val in List
       MsgBox % key "`t=`t" val
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: Insert a list of values into a looping script

30 Jul 2017, 14:55

Thanks for the reply.

Your code is interesting but I can see that my OP is confusing for what I'm after. I should not have included the 1st column of numbers as they simply represent rows.

So trying again, suppose you have the list of 7 numbers as shown in your example: List := [9,2,1,3,6,6,3], and suppose you've also got a script set to loop 7 times as shown here:

Code: Select all

F1::
List := [9,2,1,3,6,6,3]
Var := ??
SetTitleMatchMode, 2
WinActivate, Excel
Loop, 7
{
Send Var  ; What to put here???
Send {Enter}
Send {Right}
}
Return
I'm looking for a way to tie my Var to your List so that each sequential number is entered each time it loops, (ie so with the above script, the values are placed in a spreadsheet as:

A1=9, B2=2, C3=1, D4=3, E5=6, F6=6, & G7=3).

Thanks
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
Vh_
Posts: 203
Joined: 17 Mar 2017, 22:06

Re: Insert a list of values into a looping script

30 Jul 2017, 15:02

You should really look into setting Excel as a ComObject. Much more effecient. :)
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Insert a list of values into a looping script

30 Jul 2017, 15:55

Code: Select all

xl := ComObjActive("Excel.Application")
List := [9,2,1,3,6,6,3]
for i, val in List
	xl.Cells(i, i).value := val
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: Insert a list of values into a looping script  Topic is solved

31 Jul 2017, 14:14

Thanks for the responses.

Unfortunately, I'm apparently still not clarifying what I'm after because this has nothing whatsoever to do with Excel. My use of Excel in the previous post was only for the sake of example.

All is good though because I just got the solution on the forum's AHK IRC live chat page. This is what I was looking for:

Code: Select all

F1::
List := [9,2,1,3,6,6,3]
Loop, 7
{
	var:=List[A_Index]
	Send %var%
	Send {Enter}
}
Return
Thanks again however for your time and attempts at a solution.
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Insert a list of values into a looping script

31 Jul 2017, 14:52

WeThotUWasAToad wrote:Thanks for the responses.

Unfortunately, I'm apparently still not clarifying what I'm after because this has nothing whatsoever to do with Excel. My use of Excel in the previous post was only for the sake of example.

All is good though because I just got the solution on the forum's AHK IRC live chat page. This is what I was looking for:

Code: Select all

F1::
List := [9,2,1,3,6,6,3]
Loop, 7
{
	var:=List[A_Index]
	Send %var%
	Send {Enter}
}
Return
Thanks again however for your time and attempts at a solution.
A Loop and A_Index is not really needed.

Code: Select all

F1::
    List := [9,2,1,3,6,6,3]
    for key, val in List
        Send % val "{Enter}"
return
for is designed to loop through elements of an object.

You just needed to change the Msgbox to a Send and properly format from the first example I gave.

If you want to condense even further you can leave out the intermediary variable List.

Code: Select all

F2::
    for key, val in [9,2,1,3,6,6,3]
        Send % val "{Enter}"
return
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww, PsysimSV, uchihito and 205 guests