Solved

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

Posted on 2014-02-13
4
1,981 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
  • 3
4 Comments
 
LVL 51

Assisted Solution

by:Joe Winograd, EE MVE
Joe Winograd, EE MVE 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 51

Accepted Solution

by:
Joe Winograd, EE MVE 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 51

Expert Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now