Conditional Execution in Global Mapper Scripts

Global Mapper scripts can incorporate conditional logic through the use of the IF, ELSE_IF, ELSE, and END_IF commands. At a minimum, when incorporating conditional logic into a script, the user must use an IF and an END_IF command, as in the following example:

IF COMPARE_STR="%VAR1%=val1"
// Additional script commands to be run when the condition is true.
END_IF

When the script containing the sample is run, if the value of the variable %VAR1% is "val1", the statements following the IF and before the END_IF will be executed. If the value of variable %VAR1% is something other than "val1", then the statements following the IF will be skipped, and script processing will continue with the first statement after the END_IF. If the user wants to run a specific set of commands in the case where the condition specified on the IF is not true, then he can use the ELSE command:

IF COMPARE_STR="%VAR1%=val1"
// Script commands to be run when the IF condition is true.
ELSE
// Script commands to be run when the IF condition is false.
END_IF

Now, if the value of the variable %VAR1% is "val1", the statements following the IF and before the ELSE will be executed, and the commands after the ELSE and before the END_IF will be skipped. If the value of variable %VAR1% is something other than "val1", then the statements following the IF and preceding the ELSE will be skipped, and the commands after the ELSE and before the END_IF will be run.

If the user has several conditions that need to be tested, only one of which can be true, then the ELSE_IF command can be used:

IF COMPARE_STR="%VAR1%=val1"
// Script commands to be run when the IF condition is true.
ELSE_IF COMPARE_STR="%VAR1%=val2"
// Script commands to be run when the ELSE_IF condition is true.
ELSE
// Script commands to be run when the all other conditions are false.
END_IF

The commands following the IF will be handled as described above, but now there is a second condition being tested. When the value of variable %VAR1% is "val2", then the commands after the ELSE_IF and before the ELSE will be run. In the case where there are multiple IF/ELSE_IF conditions, the commands after the ELSE will be run when all of the other conditions are false.

Logical Condition Commands

The IF and the ELSE_IF commands require a COMPARE_STR parameter to specify the condition to be tested.

The user can specify multiple COMPARE_STR parameters, all of which must be true to result in the subsequent commands being executed (logical AND). Use the COMPARE_OP="ANY" parameter to specify that the subsequent commands should be run if any one of the conditions is true (logical OR).

IF

If the comparison condition is true, perform the subsequent commands. The if statement is a code block that must be closed with an END_IF.

  • COMPARE_STR - The COMPARE_STR must consist of <value><operation><value>, where either value can be a constant or a variable name (enclosed in "%"). Both values must be specified. The operation is also required, and can be one of:
    • "=" - Equals
    • "!=" - Not Equals
    • "<" - Less Than
    • "<=" - Less Than or Equal To
    • ">" - Greater Than
    • ">=" - Greater Than or Equal To
  • COMPARE_NUM - By default, the comparison will be a case-insensitive string comparison. If you want to perform a numeric comparison, specify the COMPARE_NUM="YES" parameter.

  • COMPARE_OP - If multiple compare strings (COMPARE_STR) are defined, specify the handling option for the if statement to be considered true.
    • ANY - With multiple comparisons, the result is true if one or more of the individual conditions is true (logical OR) 
    • ALL - This is the default. All comparisons must be true for the command following the if statement to be executed.

ELSE_IF

Second comparison condition can be tested, and if it is true, the subsequent command will be executed. The available parameters for are the same as IF, see above.

ELSE

All other cases that do not return true for the IF or ELSE_IF conditions will have the commands following the else statement executed.

END_IF

IF commands can be nested, so the block of commands following an IF, ELSE_IF, or ELSE command can contain another IF command:

 IF COMPARE_STR="%VAR1%=val1"
 IF COMPARE_STR="%VAR2%>10"
 // Script commands to be run when the IF condition is true.
 END_IF
ELSE
 // Script commands to be run when the IF condition is false.
END_IF