Solved

Trying to understand the colon equals (:=) symbol, especially in AutoHotkey (AHK)

Posted on 2014-02-13
4
2,662 Views
Last Modified: 2014-02-28
Hello,

I frequently use a scripting language called AutoHotkey (AHK) for creating macros. There's one particular symbol (:=) which, despite my going through the documentation on a number of occasions and asking questions in the AHK form, is just not clicking.

I've seen a few comments elsewhere indicating this symbol is used in some programming languages (and occasionally in math) but I don't know if it has the same meaning. Nevertheless, I'm posting the topic on EE now in hopes that someone might be able to provide some type of explanation or analogy — maybe from a different perspective — which will enable me to grasp the concept. By the way, I have no programming background but I do feel comfortable with Excel functions & equations (not VBA, but just the in-cell functions) as well as math (up through calculus) formulas and equations. Are there relationships or syntax in either of those areas which are analogous?

Following (between the dotted lines) are some comments which were provided by one (very patient) person on the AHK forum. Included is a quotation (in italics) from the AHK docs. (The script examples are part of the docs quotation.)

------------------------------------------------------------
"[[AHK] Variables and Expressions is] a good page for an explanation of expressions vs variables. I find this bit especially useful:

"Storing values in variables: To store a string or number in a variable, there are two methods: traditional and expression. The traditional method uses the equal sign operator (=) to assign unquoted literal strings or variables enclosed in percent signs. For example:

MyNumber = 123
MyString = This is a literal string.
CopyOfVar = %Var%  ; With the = operator, percent signs are required to retrieve a variable's contents.

Open in new window

"By contrast, the expression method uses the colon-equal operator (:=) to store numbers, quoted strings, and other types of expressions. The following examples are functionally identical to the previous ones:

MyNumber := 123
MyString := "This is a literal string."
CopyOfVar := Var  ; Unlike its counterpart in the previous section, percent signs are not used with the := operator.

Open in new window

"The latter method is preferred by many due to its greater clarity, and because it supports an expression syntax nearly identical to that in many other languages.

"Expressions are especially useful because they are required to do math and are used in function parameters."
------------------------------------------------------------

Another link which may be helpful is here: Var := expression

By the way, I don't know if this sort of cross-forum pollination is "legal" but I figure the more comments I can get, the better chance I have of getting it.  :)

Thanks
0
Comment
Question by:Steve_Brady
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 54

Assisted Solution

by:Joe Winograd, EE MVE 2015&2016
Joe Winograd, EE MVE 2015&2016 earned 500 total points
ID: 39858190
Steve,
I haven't read your whole post yet, which I will do a little later, but when I saw AutoHotkey (my favorite programming/scripting language these days), I wanted to fire off a quick response for you. The ":=" means to assign a value to a variable. The variable getting a value assigned to it is on the left of the ":=". The value being assigned is on the right, which could be as simple as an integer, or it could be a complex expression. For example, to set a counter to zero, here's a very common line of code:

Counter:=0

To increase a counter by one, here's another very common line of code:

Counter:=Counter+1

This quick note may be far too simplistic for you, so I'll read your full question more thoroughly later and will respond accordingly.

Btw, you may not know this (I didn't until a few days ago), but a new community was recently established to move the language forward. The latest release at the new community has a Windows installer, an offline help file, and a compiler that turns the AHK source code (plain text) into a stand-alone/no-install executable (an EXE file).

There is excellent documentation:
http://ahkscript.org/docs/AutoHotkey.htm

...including an alphabetical command and function index:
http://ahkscript.org/docs/commands/index.htm

...a good tutorial:
http://ahkscript.org/docs/Tutorial.htm

...and an active user forum:
http://ahkscript.org/boards/

Regards, Joe
0
 
LVL 54

Accepted Solution

by:
Joe Winograd, EE MVE 2015&2016 earned 500 total points
ID: 39858358
Steve,
Now that I've read your whole post, my previous answer stands, with a few additions. First, I agree strongly with the "greater clarity" comment. Another good example of "greater clarity" is:

Count:=Count+1
Count+=1

The above two statements produce the same result in AHK, but I much prefer the clarity of the first one.

To distinguish between the assignment operator and the Boolean "=" operator, many languages chose ":=" as the assignment operator, including Algol and Pascal. AHK managed to find a way to use both ":=" and "=" for assignments, providing some flexibility, but also some confusion to go along with it.

AHK is a great language, but has some quirks, one of which relates to the assignment operators. When parameters are fed into an AHK program, they come into variables called %1%, %2%, %3%, etc. When params come into an AHK program, I like to assign them to variables with meaningful names. So the first time I did this in an AHK script, I had these statements:

SourceFolder:=%1%
DestinationFolder:=%2%
SearchTerm:=%3%

Guess what? Didn't work! The syntax in this case must be:

SourceFolder=%1%
DestinationFolder=%2%
SearchTerm=%3%

This is no doubt because of the percent signs around the param variables, in which case AHK wants the "=" assignment operator, not the ":=" one. There are some other rare circumstances where I go with "=" for assignment, but in nearly all cases, it's ":=". For example, take a look at the AHK program in this EE article and you'll see my heavy-duty use of ":=" as the assignment operator. Regards, Joe
0
 

Author Closing Comment

by:Steve_Brady
ID: 39896377
Thanks Joe
0
 
LVL 54

Expert Comment

by:Joe Winograd, EE MVE 2015&2016
ID: 39896381
You're welcome, Steve. Happy to help.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question