Page 1 of 1
Loop read reading as plain text not recognizing variables
Posted: 11 Aug 2022, 11:01
by Stinkfoot9
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
Re: Loop read reading as plain text not recognizing variables
Posted: 11 Aug 2022, 12:59
by Rohwedder
Hallo,
provide new1.txt
Re: Loop read reading as plain text not recognizing variables Topic is solved
Posted: 11 Aug 2022, 13:34
by kaiserwilli
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
}
Re: Loop read reading as plain text not recognizing variables
Posted: 11 Aug 2022, 16:30
by Stinkfoot9
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...
Re: Loop read reading as plain text not recognizing variables
Posted: 12 Aug 2022, 07:29
by Stinkfoot9
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%
}