Still confused about when to use % with variables? This helped me figure it out

Helpful script writing tricks and HowTo's
SAbboushi
Posts: 119
Joined: 08 Dec 2014, 22:13

Still confused about when to use % with variables? This helped me figure it out

09 May 2018, 11:09

Despite the excellent and comprehensive AHK documentation and support forum, I've had some difficulty getting crystal clear on when I need to use the "% space prefix" vs. just the variable name vs. surrounding the variables in %

I wrote this script to help me figure it out and am posting in case it can help others too.

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus

User avatar
Era
Posts: 22
Joined: 28 Apr 2018, 22:36

Re: Still confused about when to use % with variables? This helped me figure it out

16 May 2018, 18:35

You and most of us. What's the use of those differently punctuated variable names? Makes no sense to me
User avatar
Cerberus
Posts: 95
Joined: 12 Jan 2016, 15:46

Re: Still confused about when to use % with variables? This helped me figure it out

29 May 2018, 18:20

Excellent for beginners! This is indeed among the most confusing inconsistencies in Autohotkey. I think the best solution is a good syntax highlighter: if you consistently highlight strings or string content as, say, yellow; special symbols that are active/working as purple (e.g. percentage signs in the right context); and the rest as white, like variable names; then it's easy for the user to get it right. The problem is that it is quite a complex task to write a proper syntax highlighter to apply these rules, because you need to classify each parameter in each command/function/operation.
icuurd12b42
Posts: 202
Joined: 14 Aug 2016, 04:08

Re: Still confused about when to use % with variables? This helped me figure it out

29 May 2018, 21:28

I reduced this to 2 rules

1) always use variable :=
2) always use %variable% to extract the content of the variable for commands that require it

Name := "John"
MsgBox, Hello %Name%, How are you today.

That's it! nothing complicated, nothing convoluting, nothing confusing. just obey these 2 rules and that will normalize your coding to a proper standard and reduce issues to basically one recurring issue which will be, Ooops I used = instead of :=, which you will instantly spot when something fishy happens.

I never use the % in any other situation than when having to use a command...

Even for text concatenation I use the . operator, because that is what I would do in 99.9999% of all programing language, though it would likely be the + operator...

Sentence := "Hello " . Name . ", How are you today."
MsgBox, %Sentence%
User avatar
nnnik
Posts: 3199
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Still confused about when to use % with variables? This helped me figure it out

29 May 2018, 23:56

I simply use a single % in commands to make them expression mode command.
Recommends AHK Studio
User avatar
jackdunning
Posts: 35
Joined: 01 Aug 2016, 18:17

Re: Still confused about when to use % with variables? This helped me figure it out

21 Aug 2018, 06:37

There are excellent reasons for occasionally forcing expressions with the % operator. The blog "Force an Expression (%) in AutoHotkey for More Powerful Commands (https://jacksautohotkeyblog.wordpress.com/force-an-expression-in-autohotkey-for-more-powerful-commands-beginning-hotkeys-part-17/) offers a beginner's explanation.

Also, for a better understanding of %var% replacement, check out the "Variable Evaluation (% Var) Versus Variable Text Replacement (%Var%)" section of https://jacksautohotkeyblog.wordpress.com/understanding-autohotkey-var-variable-text-replacement-autohotkey-tip/
Jack Dunning

I have a passion for helping new users grow maniacal about AutoHotkey Windows scripting.

Jack's AutoHotkey Blog
Free AutoHotkey Scripts and Apps for Learning Script Writing and Generating Ideas
AutoHotkey E-Books

Return to “Tutorials”

Who is online

Users browsing this forum: No registered users and 4 guests