Making decisions in scripts

Everything we do involves decision making, and scripting is no different. Sometimes you only want a command to run if some other condition is true, or you may only want to process some records in a table depending on the data they contain. ACLScript provides several methods for decision making in scripts, and they all use conditional expressions.

What is a conditional expression?

A conditional expression is any expression that evaluates to either true or false. Conditional expressions dictate which actions are taken in a script, and are specified by the script writer.

This is a fairly technical definition, but a simple real-world example can help demonstrate what it means:

Example

You are walking down the street and you see someone that you know. It is polite to greet this person, but do you say "Good morning" or do you say "Good afternoon" when you meet?

The answer depends on a simple condition: is it 12:00 PM or later? If the answer is yes, then you say "Good afternoon", otherwise you say "Good morning".

In this example, the conditional expression determines the action you take (which greeting you use) based on whether or not it evaluates to true (yes).

The expression from the preceding example could be translated into ACLScript as follows:

COMMENT checks if the current time is 12:00 PM or later
NOW() >= `t120000`

You can run this example by copying the following line, and then pasting it into the command line of Analytics. Depending on the time of day you do this, the expression evaluates to either true or false:

DISPLAY NOW() >= `t120000`

Tip

If the command line is not visible, select Window > Command Line.

Once you run the example, you can try changing the literal time value of 12:00 PM in the expression so that it evaluates to the opposite.

Deciding if a command should run

Analytics provides the IF command so that you can decide whether or not a command should run. The command requires two inputs:

  • a conditional expression
  • a command to run if the expression is true

If the conditional expression evaluates to false, then the command does not run.

Saying "Good afternoon"

Continuing with the example from above, try pasting the following code into the command line:

IF NOW() >= `t120000` DISPLAY "Good afternoon"

If the time is after 12:00 PM, then the DISPLAY command prints "Good afternoon" to the output tab. But if it is still morning where you are, then nothing is printed. The script does not run the DISPLAY command.

Saying "Good morning"

If your expression evaluated to false, then you may be wondering how to make the command print "Good morning". While some scripting languages provide an "else" construct to handle both true and false cases, ACLScript does not. Instead, you use a second IF command with the opposite expression.

Try pasting this expression into the command line:

IF NOW() < `t120000` DISPLAY "Good morning"

This example works like before, except now if the time is before 12:00 PM, then the DISPLAY command prints "Good morning".

How does this look in a script?

So far the examples have been limited to the DISPLAY command, which is only available from the command line. But in a script, the same principles apply. Instead of printing the greeting to the display tab, in this example the script stores the greeting in a variable called v_greeting:

COMMENT stores the correct greeting depending on the time of day
IF NOW() >= `t120000` ASSIGN v_greeting = "Good afternoon"
IF NOW() < `t120000` ASSIGN v_greeting = "Good morning"

If this script runs before 12:00 PM, then the value stored in the variable is "Good morning", and if it runs at 12:00 PM or later, then the value stored in the variable is "Good afternoon". Try pasting this into your script editor and running it. You can check the value of the variable on the Variables tab after it runs.

Deciding which records to process

There are times when you want to decide if the script runs a command or not, as shown above, but there are also time when you want a command to run only on certain records in a table. This is another decision making scenario, but it differs from the one that uses the IF command.

In situations where you want to selectively process records, ACLScript provides the IF parameter on many commands. When you use this approach, the command requires that you specify a conditional expression as input. The expression is tested against each record in the table, and when it evaluates to true, the record is processed:

COMMENT sums the Amount field for records that have a Quantity greater than 5
TOTAL Amount IF Quantity > 5

Calculating transactions that occur in the afternoon

You can use the same conditional expression NOW( ) >= `t120000` to calculate the sum of all transactions in a table that occurred in the afternoon. Consider the following table of transaction data:

Transaction_Amount Unit_Cost Product_No Transaction_Date Quantity
618.3 6.87 070104397 2000-11-17 12:00 90
6705.12 6.87 070104677 2000-11-17 9:30 976
7955.46 6.87 070104657 2000-11-17 14:45 1158
4870.83 6.87 070104327 2000-11-17 15:00 709
10531.71 6.87 070104377 2000-11-17 9:57 1533
5734 47 030414313 2000-10-30 1:00 122
2196 18 030414283 2000-10-30 18:25 122

To calculate the sum of the Transaction_Amount field, you use the TOTAL command:

COMMENT Sums the Transaction_Amount field
TOTAL Transaction_Amount

This command processes every record in the table and calculates a total of 38,611.42, which is the sum of all transactions.

To add some decision-making to the command, and process only those transactions that occurred at 12:00 PM or later, you add the IF parameter to TOTAL. You use the same conditional expression from the example at the start, but replace NOW( ) with the time part of the transaction date:

COMMENT Sums the Transaction_Amount field for all transactions occuring in the afternoon
COMMENT uses functions to extract the time part of the data in the Transaction_Date field
TOTAL Transaction_Amount IF CTOT(TIME(Transaction_Date)) >= `t120000`

In the command, you have to use some functions to isolate the time part of the transaction date, but once you do that, the decision is the same conditional expression from the example at the start: is it 12:00 PM or later? If the answer is yes, then the amount is included in the sum.

This command calculates a total of 15,640.59, which is the sum of all afternoon transactions in the table.