When using variables with percentages?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
852
Posts: 2
Joined: 12 Aug 2022, 13:55

When using variables with percentages?

Post by 852 » 12 Aug 2022, 14:17

Hello,
I'm using AHK already for a while, but still have problems when to use variables with percentages and when without.
Some examples:

Code: Select all

EnvGet, var, %var%
Works as expected, reading with %, writing without.

Code: Select all

StringReplace, str, str, %separator%
Writing output variable without, but reading also while reading separator needs it.

Code: Select all

If InstallPath
IfExist %InstallPath%

Code: Select all

Loop, read, %FileName%
Loop, parse, A_LoopReadLine, CSV
Similar functions, every time different.

Is there a rule which I don't understand? I really like AHK , but try and error to find out how to use variables is very annoying. Any hints about that? Thanks already :-)

User avatar
joedf
Posts: 8958
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: When using variables with percentages?

Post by joedf » 12 Aug 2022, 14:52

Yeah, it can be a bit confusing... especially early on. Maybe this will help?
"When to use percents": https://www.autohotkey.com/docs/Tutorial.htm#s61
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

852
Posts: 2
Joined: 12 Aug 2022, 13:55

Re: When using variables with percentages?

Post by 852 » 12 Aug 2022, 15:58

Unfortunately not really. I've read the help, but it doesn't feel to work like described.
When not to use percent signs:
In parameters that are input or output variables
This explains the case of StringReplace. But in the example of EnvGet it's also an input variable which need percents.
I don't see a difference between the input variables for both loops as well. Why are some "more input" than others?
When not to use percent signs:
On the left side of legacy (non-expression) if-statements
Explains the behavior of IfExists because it isn't an if-statement although it looks like one. Unintuitive, but at least this one I understand.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: When using variables with percentages?

Post by BoBo » 12 Aug 2022, 16:13

@852 - the first thing you shouldn't do is to use deprecated commands. Those are only kept in place to provide backward compatibility to run old/outdated/unconverted scripts. That will solve instantly 50% of the problems mentioned above - as functions used instead are 'expression-styled'.

Code: Select all

Loop, read, %FileName%
Loop, parse, A_LoopReadLine, CSV
Similar functions, every time different.
Well, like shoes, but for different environments. Would you prefer sandals if walking through a pond of mud?
Loop, read - expects a static string parameter pointing to some content, so using a variable instead must be declared/made obvious in some way.
Loop, parse - expects the content itself, using a variable by default, no need to declaring it explicitly, right? Right.

Program languages are like wifes/husbands, you don't have to understand everything they say, simply accept it and doing it their way. ;)

Post Reply

Return to “Ask for Help (v1)”