Comparing Variables and Strings with AutoIt

Tutorial on how to compare variables and strings using conditional statements.

4926 views

Edited: 2017-03-26 15:40

When you want to compare variables and strings, you will be using an if statement, sometimes combined with other code to accomplish the comparison that you wish for.

In many cases, when someone wants to compare two variables, it will be a matter of performing a simple if check – but often you will also want to perform some simple math on the variables, before they are checked. This can all be done in the same conditional statement, or you can do it before – its entirely up to you.

Simple Comparison in AutoIt

In this script, we will be comparing two variables to see if they match. Its recommended that you test this for yourself to get a better understanding of how conditional statements work. Create a new AutoIt script, then copy and paste the following code into SciTE, or whatever editor you are using. I.e.

$variable1 = "whatever"
$variable2 = "whatever"
If $variable1 = $variable2 Then
    MsgBox(4096, "Message", "The variables matched..")
EndIf

The above script will create a MsgBox, with the message The variables matched. Currently it will not do anything if the variables didn't match – so to make it return a different message on failure, we will be adding an Else case to the if check. I.e.

$variable1 = "whatever"
$variable2 = "whatever"
If $variable1 = $variable2 Then
    MsgBox(4096, "Message", "The variables matched..")
Else
    MsgBox(4096, "Message", "No Match")
EndIf

The above will produce a MsgBox with a message saying: No Match if the variables we compare do not match.

Comparison Operators

When we compare things trough scripting, we will be using different operators depending on what we want to know. In addition to the equals sign, there are also signs which allows you to ask if a given value is less than, or greater than another value. The Comparison Operators in AutoIt are as follows:

= Check if two values match. Case insensitive when dealing with strings.
== Check if two values match. Case sensitive when dealing with strings.
<> Not Equal To. The comparison is case-insensitive when dealing with strings.
< Checks if the first value is less than the second.
> Checks if the first value is greater than the second.
<= Checks if the first value is less than, or equal to the second value.
>= Checks if the first value is greater than, or equal to the second value.

Tell us what you think:

  1. How to declare and work with variables in AutoIt, as well as some background information.
  2. Everything you need to know about working with minimized windows.
  3. How to set the request headers when performing HTTP requests.
  4. This Tutorial will focus on post requests in AutoIt, using the Winhttprequest.5.1 object.
  5. How to use while, for, and do until loops, and how to loop through arrays and object in AutoIt.

More in: AutoIt Tutorials