The if else statements control what actions are performed based on the results of a conditional test. The syntax is as follows:
if (<conditional_test>) {<statements>} [else {<statements>}]
The else portion of the statement is optional.
The braces around the statements are required, even if there is only one statement.
In the following example, the if statements check whether a value to use in a table exists as a key or not. Each key in a table must be unique. In the script, if a key exists, the value associated with the key is added to the new value.
This script uses two if statements.
-
The first if statement tests whether any of the existing keys match the name of the animal ready to be loaded into the table. If the animal name matches any of the existing keys, a flag is set to TRUE.
-
The second if statement tests whether the flag is FALSE. If the flag is FALSE, the animal name and number are added to the table. If the flag is TRUE, the number of animals are added to the animal count already stored in the table.
ASL Script (if_do.asl): z = table(); START { rep(IMPORT) } IMPORT { animal:word count:integer eol } do { test="FALSE"; foreach i (z) { if (i == animal) { test="TRUE"; } } if (test=="FALSE") { z[animal]=count; } else { z[animal]=z[animal]+count; } } EOF do { foreach i (z) { print(i." count ".z[i]); } } Input (if_do.txt): dog 3 cat 4 canary 3 cat 2 dog 5 dog 1 cat 1 Output: $ sm_adapter --file=if_do.txt if_do.asl dog count 9 cat count 7 canary count 3 $