if
Overview
The if
statement is used to conditionally execute one or more statements
Syntax
Conditional Expressions
A conditional expression (hereafter referred to as simple an expression) is evaluated to provide a TRUE or FALSE result which in turn determines whether one or more statements are to be executed or not. The following are examples of a valid expression:
An expression used by the if
statement may contain:
Numeric and string literals
Regular expressions
Variables
Operators
Functions
Numeric and string literals
A literal is a specified value, such as 4.5
or "hostname"
. Literals may be numbers or strings (text).
If a literal is non-quoted then it will be treated as a number if it represents a valid decimal integer or floating point number (in either regular or scientific notation), else it will be treated as a string.
If a literal is quoted then it is always treated as a string, thus 3.1515926
is a number and "3.1415926"
is a string.
Regular expressions
Regular expressions must be enclosed within forward slashes (/
), and are assumed to be in ECMAScript format.
If present, a regular expression must be used on the right hand side of either an !~
or an =~
operator, and when evaluated it will be applied to the value on the left hand side of an operator, eg:
As the forward slash is used as a delimiter for the expression, any literal forward slashes required by the expression should be escaped with a back-slash: \/
Variables
Variables can be used within expressions, in which case they are replaced with their values. Once expanded, these values are treated as literals.
Operators
Operators are evaluated according to the operator precedence rules in the table below (where the highest precedence is evaluated first), unless parentheses are used to override them. Operators with the same precedence are evaluated from left to right.
Precedence
Operator
Meaning
1
!
Unary negation
2
*
Multiplication
2
/
Division
2
%
Modulo
3
+
Addition
3
-
Subtraction
4
<
Less than
4
<=
Less than or equal to
4
>
Greater than
4
>=
Greater than or equal to
5
==
Is equal to
5
!=
Is not equal to
5
=~
Matches regular expression
5
!~
Does not match regular expression
6
&&
Boolean AND
7
`
`
Boolean OR
Although expressions are evaluated based on the precedence of each operator as listed in the above table, it is recommended that parenthesis are used within the expression in order to remove any ambiguity on the part of a future reader.
Functions
A function is used to evaluate one or more arguments and return a result which is then taken into consideration when evaluating the overall truth of the expression.
Function calls start with a the character @
which is followed by the function name and a comma separated list of parenthesised parameters, for example @MIN(1, 2, 3)
.
Function names must be specified in UPPER CASE as shown in the examples below.
The following functions are supported by the if
statement:
Numeric functions
MIN
Return the smallest number from the specified list (requires at least 2 arguments)
Examples:
@MIN(1,2)
returns1
@MIN(1,2,-3)
returns-3
@MIN(1,2,"-1")
returns-1
- string"-1"
is converted to number-1
@MIN(1,2,3/6)
returns0.5
@MIN(1,2,"3/6")
returns1
- string"3/6"
is converted to number3
, up to first invalid character@MIN(1,2,"zzz")
returns0
- string"zzz"
is converted to number0
MAX
Return the largest number from the specified list (requires at least 2 arguments)
Examples:
@MAX(1,2)
returns2
@MAX(-1,-2,-3)
returns-1
@MAX(1,2,100/10)
returns10
ROUND
Returns number rounded to digits decimal places. If the digits argument is not specified then the function will round to the nearest integer.
This function rounds half away from zero, e.g. 0.5
is rounded to 1
, and -0.5
is rounded to -1
Examples:
@ROUND(3.1415,3)
returns3.142
@ROUND(3.1415,2)
returns3.14
@ROUND(3.1415926536,6)
returns3.141593
@ROUND(3.1415)
returns3
@ROUND(2.71828)
returns3
String functions
CONCAT
This function will treat all its arguments as strings, concatenate them and return the result.
Examples:
@CONCAT("the answer ", "is")
returnsthe answer is
@CONCAT("the answer ", "is", " 42")
returnsthe answer is 42
@CONCAT("the answer ", "is", " ", 42)
returnsthe answer is 42
SUBSTR
Return a sub-string of string, starting from the character at position start
and continuing until the end of the string end until the character at position length
, whichever is shorter.
If length is omitted, then the portion of the string starting at position start and ending at the end of the string is returned.
Examples:
@SUBSTR("abcdef", 1)
returnsabcdef
@SUBSTR("abcdef", 3)
returnscdef
@SUBSTR("abcdef", 3, 2)
returnscd
@SUBSTR("abcdef", 3, 64)
returnscdef
STRLEN
Returns the length of its argument in bytes.
Examples:
@STRLEN("foo")
returns 3@STRLEN(@CONCAT("ab", "cd"))
returns 4@STRLEN(1000000)
returns 7 (the number1000000
is treated as a string)
PAD
This function returns value, left-padded with pad_char (0
by default) up to specified width. If width is less than or equal to the width of value, no padding occurs.
Examples:
@PAD(5, 123)
returns00123
@PAD(5, 12345)
returns12345
@PAD(1, 12345)
returns12345
@PAD(5, top, Z)
returnsZZtop
EXTRACT_BEFORE
This function returns the substring of string
that precedes the pattern
. If pattern
cannot be found in the string
, or either string
or pattern
are empty, result of the function is empty string.
Examples:
@EXTRACT_BEFORE("abcdef", "d")
returnsab
@EXTRACT_BEFORE("abcbc", "bc")
returnsa
@EXTRACT_BEFORE("abcdef", "x")
returns empty string
EXTRACT_AFTER
This function returns the substring of string
that follows the pattern
. If pattern
cannot be found in the string
, or either string
or pattern
are empty, result of the function is empty string.
Examples:
@EXTRACT_AFTER("abcdef", "cd")
returnsef
@EXTRACT_AFTER("abcabc", "ab")
returnscabc
@EXTRACT_AFTER("abcdef", "abb")
returns empty string
EXTRACT_XXX functions can be combined to extract the middle part of the string, for example @EXTRACT_AFTER(@EXTRACT_BEFORE("abcdef", "ef"), "ab")
returns cd
.
Date functions
All date functions operate with dates in yyyyMMdd format
CURDATE
Returns the current (actual) date in the timezone of the Exivity server. The format may be any valid combination of strftime specifiers. The default format is %Y%m%d
which returns a date in yyyyMMdd format.
Examples (assuming run date is 1 July 2019, at 12:34:56):
@CURDATE()
returns20190701
@CURDATE(\"%d-%b-%y\")
returns01-Jul-19
@CURDATE("%H:%M:%S")
returns12:34:56
@CURDATE("%u")
returns1
(weekday - Monday)@CURDATE("%j")
returns182
(day of the year)
DATEADD
Adds a specified number of days to the given date, returning the result as a yyyyMMdd
date.
Invalid dates are normalised, where possible (see example below):
Examples:
@DATEADD(20180101, 31)
returns20180201
@DATEADD(20180101, 1)
returns20180102
@DATEADD(20171232, 1)
returns20180102
(the invalid date20171232
is normalised to20180101
)@DATEADD(20180101, 365)
returns20190101
DATEDIFF
Returns the difference in days between two yyyyMMdd dates. A positive result means that date1 is later than date2. A negative result means that date2 is later than date1. A result of 0 means that the two dates are the same.
Invalid dates are normalised, when possible (see example below):
Examples:
@DATEDIFF(20190101, 20180101)
returns365
@DATEDIFF(20180201, 20180101)
returns31
@DATEDIFF(20180102, 20180101)
returns1
@DATEDIFF(20180101, 20180102)
returns-1
@DATEDIFF(20180101, 20180101)
returns0
@DATEDIFF(20171232, 20180101)
returns0
(the invalid date20171232
is normalised to20180101
)
DTADD
This function adds count number of units (DAYS
by default) to the specified datetime value and return normalised result datetime value in YYYYMMDDhhmmss
format.
Datetime can be in any of the following formats:
YYYYMMDD
YYYYMMDDhh
YYYYMMDDhhmm
YYYYMMDDhhmmss
All missing bits of datetime value assumed zeros.
Supported units are (both singular and plural spellings supported):
YEAR
MONTH
DAY
(default)HOUR
MINUTE
SECOND
Example
@DTADD(20190701, 2)
returns20190703000000
@DTADD(20190701, 2, HOURS)
returns20190701020000
@DTADD(2019070112, 50, DAYS)
returns20190820120000
@DTADD(20190701123456, 10, MONTH)
returns20200501123456
Transcript-specific functions
Transcript-specific functions may be preceded with an exclamation mark in order to negate their output. For example:
FILE_EXISTS
Returns 1 if the file filename exists, else returns 0.
FILE_EMPTY
In strict mode, this function returns 1 if the file filename exists and is empty. If the file does not exist, then this is considered an error.
In permissive mode, a non-existent file is considered equivalent to an existing empty file.
In either case, if the file exists and is not empty, the function returns 0
FILE_EXISTS and FILE_EMPTY functions will only check files within Exivity home directory and its sub-directories, filename must contain pathname relative to Exivity home directory.
DSET_EXISTS
Returns 1 if the specified DSET exists, else 0
DSET_EMPTY
In strict mode (option mode = strict
), this function returns 1 if the specified DSET exists and is empty. If the DSET does not exist, then this is considered an error.
In permissive mode (option mode = permissive
), a non-existent DSET is considered equivalent to an existing empty DSET.
In either case, if the DSET exists and is not empty, the function returns 0.
COLUMN_EXISTS
This function returns 1 if the specified column exists, else 0. The column name may be fully-qualified, but if it is not, then it is assumed to be in the default DSET.
DSET_ROWCOUNT
This function returns number of rows within specified DSET.
In permissive mode (option mode = permissive
), a non-existent DSET is considered equivalent to an existing empty DSET, and zero is returned.
DSET_COLCOUNT
This function returns number of columns within specified DSET.
In permissive mode (option mode = permissive
), a non-existent DSET is considered equivalent to an existing empty DSET, and zero is returned.
Last updated