Revised December 2019
IFE(expr1,expr2{,flags})
IFE$(expr1$,expr2${,flags})
The IFE and IFE$ functions provide a shorthand way to select one of two values without having to resort to an IF/THEN/ELSE statement. Each returns the value of either the first or second expression argument, depending on the flags. IFE returns a numeric value, while IFE$ returns a string value.
Without the flags parameter, these act as follows:
IFE(expr1,expr2) ! if expr1 then expr1 else expr2
IFE$(expr1$,expr2$) ! if expr1$ # "" and # .NULL then expr1$ else expr2$r2$
In other words, if the first expression is non-zero (for the numeric version) or non-blank and non-null (for the string version), then the return value of the function is the first expression; else it is the second expression.
The optional flags are defined in ashell.def and may be used to alter the type of test performed on the first expression:
Symbol |
Value |
Description |
---|---|---|
IFE_NB |
0 |
IFE$() default (test if expr1$ "Not Blank") |
IFE_NZB |
1 |
Treat "0" same as blanks |
IFE_TRUE |
0 |
IFE() default (test if expr1 TRUE) |
IFE_FALSE |
2 |
Test if expr FALSE (zero) instead of TRUE |
For example:
IFE(expr1,expr2,IFE_FALSE) ! if expr1 = 0 then expr1 else expr2
IFE$(expr1$,expr2$,IFE_NZB) ! if expr1$ # "0" and expr1$ # "" then expr1$ else expr2$
Note that consistent with the way ASB tests for string equality, trailing blanks are not significant. Also note that .NULL here is treated the same as "" for IFE$ and as 0 for IFE; see History below.
IFE and IFE$ are similar to the IFELSE and IFELSE$ functions, except:
• | Whereas IFELSE/IFELSE$ uses a separate relative expression argument to decide which of the two possibilities to return, IFE/IFE$ uses the first expression both as the test expression and as a return value (if the test passes). |
• | Unlike IFELSE and IFELSE$ which always evaluates each of the arguments, IFE and IFE$ do not evaluate the second expression unless the test on first expression fails (i.e. unless the second expression is to be returned.) This is particulary useful when the second expression is a function call or other complex expression with significant overhead and/or side effects.) |
Additional Examples
sale'price = IFE(special'price,standard'price)
The above is equivalent to:
if special'price <> 0 then ! if there is a special going
sale'price = special'price ! use the special price
else
sale'price = standard'price ! else use the regular price
endif
The following example uses sets salutation$ to "Dear <firstname>" if the we know the first name, otherwise it uses the more generic "Dear Customer."
salutation$ = "Dear " + IFE$(firstname$,"Customer")
The expression arguments can be anything that could appear on the right side of an assignment, such as a function call, provided they are numeric expressions in the case of IFE, and string expressions in the case of IFE$.
In the next example, we attempt to email the receipt but if the return status is not zero, indicating failure, we print it instead. The return value of the IFE function will be the return value of the Fn'Email'Receipt() function (if 0), else the return value of the Fn'Print'Receipt() function. Note that since this is backwards from the default mode—where the second expression is only evaluated if the first one is 0, FALSE, or blank—we add the IFE_FALSE flag.
status = IFE(Fn'Email(receipt), Fn'Print(receipt), IFE_FALSE)
Perhaps the place where the IFE functions—along with the related IFELSE functions—are most useful is in argument lists with conditional dependencies where you would otherwise have to use temporary variables, as in this example where we want to use the employee's from address if she has one, else use the company return address:
status = Fn'Email(to=toaddr$, from=IFE$(emp.addr$,"sinkhole@acme.com"), body=msg$)
See Also
History
2023 March, A-Shell 6.5.1728: IFE$ now treats .NULL the same as "".
2019 March, A-Shell 6.5.1657: IFE and IFE$ added to A-Shell.