TSolo315 MturkCrowd
Six years of turking experience

# Expressions

Doing math on variables

1

What is an Expression?

An expression is used to perform operations on variables. You can perform operations with numbers, such as simple addition, subtraction, multiplication, etc. You can also use expressions with strings to alter text content. We have already seen several examples of expressions involving strings:

``````
clipboard := "my text"   ; Give the clipboard entirely new contents.
MyNewVar := MyVar1 MyVar2 ; Combine two variables containing text into one new variable.
clipboard := "" ; Clear the clipboard.
```
```

The := symbol grouping, that you have seen numerous times at this point, is an indicator that an expression is taking place and that the result is being stored in a variable. In an expression like this variables are not enclosed in percentage signs and plain text must always be surrounded by quotation marks. Outside of expressions you need to surround variable names in percentage signs BECAUSE text does not need to be enclosed in quotes. This allows AHK to differentiate between sending the literal variable name text or the contents of the variable.

``````
^1::
MyVar1 := "This is my new variable." ; Set MyVar1 to hold the text: This is my new variable
sendinput %MyVar1% ; send the text: This is my new variable.
sendinput {enter}
MyVar1 := MyVar1 " There are no percentage signs here" ; Append the text: There are no percentage signs here to the end of the variables text content.
sendinput %MyVar1% ; Send the text: This is my new variable. There are no percentage signs here
sendinput {enter}
sendinput If it is not an expression you do not need to surround text in quotes ; Send this exact text
sendinput {enter}
sendinput %MyVar1% and you can mix variables and text when using sendinput. ; Send the text: This is my new variable. There are no percentage signs here and you can mix variables and text when using sendinput.
return
```
```
2

Expressions and Numbers

Expressions can be used to perform operations on numbers. This is done by using what is known as an operator. Here are some examples of common operators used with numbers:

• The + operator is used for addition.
• The - operator is used for subtraction.
• The * operator is used for multiplication.
• The / operator is used for division.
• The ** operator is used to power.

Here is an example of expressions involving numbers:

``````
^1::
click 2
clipboard = ""
sendinput ^c
clipwait, 5
originalnumber := clipboard
numbersubfive := originalnumber - 5
numberdivfive := originalnumber / 5
numbermultfive := originalnumber * 5
return

^2::
sendinput The number plus five is: %numberaddfive%
sendinput {enter}
sendinput The number minus five is: %numbersubfive%
sendinput {enter}
sendinput The number divided by five is: %numberdivfive%
sendinput {enter}
sendinput The number multiplied by five is: %numbermultfive%
sendinput {enter}
sendinput The original number was: %originalnumber%
originalnumber := ""
numbersubfive := ""
numberdivfive := ""
numbermultfive := ""
return
```
```

With the first hotkey a number under the cursor is copied and saved to a new variable. Four expressions are then done on that variable with the results each being saved to a new variable. With the second hotkey the results are sent and the variables are cleared.

3

Expressions and Strings

The only common operator used on strings is the . operator, and for the most part, it is optional. The . operator is used to combine strings together, and must have a single space on each side.

``````
^1::
MyVar1 := "This is a variable with text. "
MyVar2 := "This is another text variable."
MyVar3 := MyVar1 . MyVar2 ; This is a variable with text. This is another text variable.
MyVar4 := "This is plain text and " . MyVar1 ; This is plain text and This is a variable with text.
return
```
```
4

True and False

In AHK there are two boolean values, true and false. These are important when using if statements, which we will cover in a later tutorial. You can use an expression to evaluate if something is true or false. For example, if you had a counter variable set up that would increment by one every time you pressed a hotkey, you could use an expression like: countermax := counter > 10 to check if your counter was greater than 10. If your counter variable was indeed greater than 10 the countermax variable would be set to true. Otherwise it would be set to false. There are several comparison operators:

• The > operator is used for greater than.
• The >= operator is used for greater than or equal to.
• The < operator is used for less than.
• The <= operator is used for less than or equal to.
• The = operator is used for equal to.
• The != operator is used for not equal to.
5

Practical Example

``````
f1::
click 2 ; Double click.
clipboard := "" ; Empty the clipboard.
sendinput ^c ; Copy highlighted number.
clipwait, 5 ; Wait for text to be copied.
result := clipboard ; Set result variable to clipboard contents.
result := "The number of followers is: " (result - 100) ; Set result variable to "The number of followers is: " plus the result number minus one hundred.
sendinput ^w ; Close tab.
sleep 500 ; Short sleep.
sendinput {tab} ; Tab press.
sendinput %result% ; Send result variable.
result := "" ; Clear the result variable.
return
```
```

Imagine a HIT that asked you to go to a webpage and then return the number of followers on that webpage minus one hundred. This script would allow you to simply hover your cursor over the number of followers, press the hotkey, and allow the script to handle the rest.