Initialize dynamic variables from files names

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
taiseiyou
Posts: 3
Joined: 23 Mar 2023, 12:06

Initialize dynamic variables from files names

Post by taiseiyou » 23 Mar 2023, 12:32

Hello, I am trying to convert an AHK1 script of mine to AHK2 and I am getting hung up on the issue of initializing dynamic variables. In my case, I have a folder full of text files. I want to create dynamic variables based on the text file names. For example, based on a directory containing Texfile.txt, Dexfile.txt, and Hexfile.txt, I am using the following dynamic variable, List%X%, to create variables such as ListTexfile, ListDexfile, ListHexfile, etc. This worked fine in AHK1 when variables could be initialized on the fly, but I am having trouble initializing the dynamic variables before referring to them in AHK2. I don't want to hard code the variable names into my script because my script is designed to initialize new variables based on the names of files that may get added to the directory later.

The problematic code snippet is this:

Code: Select all


Loop Files, "Data\Source\*.txt"
	{
		SplitPath(A_LoopFileName, , , , &X) ; File name is assigned to X, e.g. "dexfile"
		RandomRead(X) ; Send file name to RandomRead function

	}

RandomRead(N){
Global

		List%N% := Fileread("Data\Source\" . N . ".txt") ; Assign the text in Source\N.txt to the variable ListN, e.g. the data in Source\Dexfile.txt is assigned to ListDexfile
		Array%N% := StrSplit(List%N%,"`n") ; Split each line in the file into an array item
		rand := Random(1, Array%N%.Length) ; Select a random line of text from the array
		Syntax := StrReplace(Syntax, "[" . N . "]", Array%N%[rand]) ; Replace any bracketed text inside Syntax with the same filename (e.g. [Dexfile]) with a random line from the text file with the same name

}
This allows me to create a text file at will and reference it by using something like [Dexfile], in an external file that will be read by AHK.

I seem to have gotten my code up to speed besides this. I liked being able to spontaneously create dynamic variables in AHK1, but it seems like my approach doesn't utilize best practices. If anyone could help me update this code, I would greatly appreciate it.

Thank you

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Initialize dynamic variables from files names

Post by mikeyww » 23 Mar 2023, 13:13

Welcome to this AutoHotkey forum!

Just one idea is that you could use a map object.

Code: Select all

#Requires AutoHotkey v2.0
list    := Map()
x       := 'Dex'
list[x] := 5
MsgBox list['Dex']

User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Initialize dynamic variables from files names

Post by boiler » 23 Mar 2023, 13:13

Use a Map/Associative Array. You can use the file name as the key. For example:

Code: Select all

List[N] := FileRead("Data\Source\" . N . ".txt")

Edit: What mikeyww said. :D

taiseiyou
Posts: 3
Joined: 23 Mar 2023, 12:06

Re: Initialize dynamic variables from files names

Post by taiseiyou » 23 Mar 2023, 13:16

Thank you both. I'll investigate this. Cheers!

taiseiyou
Posts: 3
Joined: 23 Mar 2023, 12:06

Re: Initialize dynamic variables from files names

Post by taiseiyou » 23 Mar 2023, 15:24

I was able to successfully update my code as follows:

Code: Select all

TempArray := [] ; Initialize an empty array
Loop Files, "Data\Source\*.txt" { ; For each txt file in the directory...
	SplitPath(A_LoopFileName, , , , &Filename) ; get the name without extension and assign it to variable
	Loop read, A_LoopFilePath { ; Read each line...
		TempArray.push(A_LoopReadLine) ; and add it to the array
	}
	rand := Random(1, TempArray.Length) ; Generate a random number between 1 and the num of array items
	Syntax := StrReplace(Syntax, "[" . Filename . "]", TempArray[rand]) ; replace string in Syntax with the random line
	TempArray := [] ; Clear the array
}
Thanks for the help!

Post Reply

Return to “Ask for Help (v2)”