Simple Syntax for Looping Through a List of Variables? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Simple Syntax for Looping Through a List of Variables?  Topic is solved

09 Feb 2017, 14:20

Okay, in pseudocode this is what I want to do:

Code: Select all

For each VariableNameString in Array
	FileAppend, % VariableNameString . "=" . %VariableNameString% . "`n", % SaveFile.txt
Basically the idea is I have an array that contains strings with the variable names of variables I want to save the contents of to a file. Assuming the array contains [ "UserName", "UserMode", "NumberOfThings" ], then at the end the contents of the file would look like this:

Code: Select all

UserName=MyUser
UserMode=2
NumberOfThings=15
Is there a straightforward way to do this? I looked at the documentation for a for-loop and I can't figure out how to make it do what I want.

EDIT: I figured this out, I think. I thought that you had to use a key value pair object; I didn't realize a normal array would work, and I also didn't realize that "key" is the index of the array, not the value at that index. Which... should be obvious I suppose... I'm just bad at objects. ^^;

If anyone wants working code for this, it looks like this:

Code: Select all

VarsToSave := ["Variable1", "Variable2", "Variable3"]
For Key, Value in VarsToSave
	FileAppend, % Value . "=" . %Value% . "`n", % SaveFile.txt
Ironically my pseudocode was really close...
FanaticGuru
Posts: 1508
Joined: 30 Sep 2013, 22:25

Re: Simple Syntax for Looping Through a List of Variables?

09 Feb 2017, 15:15

Code: Select all

UserName=MyUser
UserMode=2
NumberOfThings=15
VarList := ["UserName", "UserMode", "NumberOfThings"]
for key, Var in VarList
	MsgBox % Var " = " %Var%
FG

Edit: In the 2 minutes it took me to post you edited with your solutions. Which is very very close to mine.
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Simple Syntax for Looping Through a List of Variables?

09 Feb 2017, 15:46

For anyone who is curious, the second half of this is to put the following in the autoexecute section of the script:

Code: Select all

IfExist, % SaveFile	; Load saved items if there are any
{
	Loop, Read, % SaveFile
	{
		ValueStep := false
		Loop, Parse, A_LoopReadLine, =
		{
			if (!ValueStep)
				VarToCreate = %A_LoopField%
			else
				%VarToCreate% = %A_LoopField%
			ValueStep := !ValueStep
		}
	}
	FileDelete, % SaveFile
}
Do that, and any variable listed in the VarsToSave array will be saved when the script is exited, and reloaded when it is opened again. Handy. :)
anidealworld
Posts: 3
Joined: 17 Feb 2017, 13:07

Re: Simple Syntax for Looping Through a List of Variables?

17 Feb 2017, 13:11

What about an array of strings? What is the syntax for that?
i.e. I can't get this to work:

Code: Select all

urls[test1] ="asdasd,asdasdasd,asdasdasd,asdasd"
urls[test2] ="zxczxc,zxczxc,zxczxczxc"
value:= urls[test1]
msgbox, %value%
Really what I what I want to do is use the comma as a delimiter and search each string similar to above, but only in "test1" or "test2", etc. (I have 10+ in my actual script) depending on what variable is passed to the function. But I can't get the syntax right.
User avatar
Xtra
Posts: 2101
Joined: 02 Oct 2015, 12:15

Re: Simple Syntax for Looping Through a List of Variables?

17 Feb 2017, 13:20

anidealworld wrote: But I can't get the syntax right.

Code: Select all

urls := {}

urls["test1"] := "asdasd,asdasdasd,asdasdasd,asdasd"
urls["test2"] := "zxczxc,zxczxc,zxczxczxc"
value := urls["test1"]
msgbox, %value%
HTH

Return to “Ask For Help”

Who is online

Users browsing this forum: BartmanEH, boiler, djuga, DMDComposer, lupenhagen, Pepineros, tdalon, XMCQCX, zhu25 and 52 guests