Loop read reading as plain text not recognizing variables Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Stinkfoot9
Posts: 51
Joined: 29 May 2022, 12:17

Loop read reading as plain text not recognizing variables

Post by Stinkfoot9 » 11 Aug 2022, 11:01

Hello again

I'm trying to read lines from a file and have them be recognized as a variable I have defined in the script, right now they are being read as plain text ignoring the variables I have defined above.

Probably pretty simple right? thanks for the help.

Code: Select all


	Run = Run, Firefox.exe ; <---------------------- Variable
	Loop, read,C:\Program Files\AutoHotkey\new1.txt ; <------- firstline in file is %run%
	{
	count = 1,2,3,4
	Ind1 := A_index
	Loop, parse, A_LoopReadLine, `n
	{
		If Ind1 in %count%
		{                                                                              
		word_array := StrSplit(A_loopfield,"`n", "")
		run = run123 ; <--------------------------- Variable
		MsgBox %  word_array[a_index] ; <------ reads as plain text from file not as variables above.
		}                                                           
		else
		{
		run = %A_loopreadline%  ; <------- Variable
		Msgbox hi2 %Ind1% %run% ; <------ reads as plain text from file not as variables above.
		}                                               
	    }
	}
        return
Last edited by Stinkfoot9 on 11 Aug 2022, 14:02, edited 1 time in total.

Rohwedder
Posts: 7551
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Loop read reading as plain text not recognizing variables

Post by Rohwedder » 11 Aug 2022, 12:59

Hallo,
provide new1.txt

kaiserwilli
Posts: 3
Joined: 01 Aug 2022, 15:59

Re: Loop read reading as plain text not recognizing variables  Topic is solved

Post by kaiserwilli » 11 Aug 2022, 13:34

If you want to read a string as a variable, you can use either = % %VariableWithString% or := %VariableWithString%. I believe that the second method is preferred.

It also seems like you're using Loop, parse to try to loop through a file by new lines, but loop read already does single lines so the code is redundant.

Code: Select all

Run = Run, Firefox.exe ; <---------------------- Variable
	Loop, read,C:\Program Files\AutoHotkey\AHKS\new1.txt  ; <------- firstline in file is %run%
	{
		Variable = % %A_LoopReadLine%
		msgbox %Variable%
		;or
		Variable := %A_LoopReadLine%
		msgbox %Variable%
	}
return

The above code will attempt to check the value of each line. if the line isn't a variable reference, then you will get an error since it doesn't check if a value is enclosed in a percent sign.
The way that I can think to do what you are attempting is with searching the strings for the variable percent signs, then replacing the value. This function should work to find all variables in string and convert them to their value in the script. You can use the function on A_LoopReadLine to convert the variables.

Code: Select all

Run = Run, Firefox.exe ; <---------------------- Variable
Message = This is a message

StringWithLiteralVariables := "%Run%`n%Message%"

msgbox % StringWithLiteralVariables "`n`n`nConverts to`n`n`n" ReplaceAllVariablesInString(StringWithLiteralVariables)
return

ReplaceAllVariablesInString(string) ;replaces literal percent enclosed variables in strings with the variable value defined in the script
{
	global ;must be global to use the variables defined other places in the script
	local FoundPos
	Loop
	{
		FoundPos := RegExMatch(string,"%([A-z0-9]*)%",MatchVariable) ;regex match to find any letters or numbers enclosed in percent signs
		if !FoundPos
			break
		string := RegExReplace(string,MatchVariable,%MatchVariable1%) ;replaces the full match with the variable value of the match inside the percent
	}
	return String
}

User avatar
Stinkfoot9
Posts: 51
Joined: 29 May 2022, 12:17

Re: Loop read reading as plain text not recognizing variables

Post by Stinkfoot9 » 11 Aug 2022, 16:30

kaiserwilli wrote:
11 Aug 2022, 13:34
If you want to read a string as a variable, you can use either = % %VariableWithString% or := %VariableWithString%. I believe that the second method is preferred.

It also seems like you're using Loop, parse to try to loop through a file by new lines, but loop read already does single lines so the code is redundant.



The above code will attempt to check the value of each line. if the line isn't a variable reference, then you will get an error since it doesn't check if a value is enclosed in a percent sign.
The way that I can think to do what you are attempting is with searching the strings for the variable percent signs, then replacing the value. This function should work to find all variables in string and convert them to their value in the script. You can use the function on A_LoopReadLine to convert the variables.



I was just about to tell you about the error code thanks for the solution...
Last edited by Stinkfoot9 on 12 Aug 2022, 11:07, edited 1 time in total.

User avatar
Stinkfoot9
Posts: 51
Joined: 29 May 2022, 12:17

Re: Loop read reading as plain text not recognizing variables

Post by Stinkfoot9 » 12 Aug 2022, 07:29

kaiserwilli wrote:
11 Aug 2022, 13:34



The above code will attempt to check the value of each line. if the line isn't a variable reference, then you will get an error since it doesn't check if a value is enclosed in a percent sign.
The way that I can think to do what you are attempting is with searching the strings for the variable percent signs, then replacing the value. This function should work to find all variables in string and convert them to their value in the script. You can use the function on A_LoopReadLine to convert the variables.
Marked My Wanted Var Inside the Text File With A "+" Symbol

Code: Select all

		dog = %A_loopreadline%
		If instr(dog,"+") 
			{
			Stringreplace, cat, dog, +, ,all)      
			x1 = % %cat%	 
			msgbox %x1%
			}
		Else
			 {
			 x1= %A_loopreadline%
			 msgbox %x1%
			}

Post Reply

Return to “Ask for Help (v1)”