Skip to main content

IFS

Returns a value for each dynamic array element based on the truth value of that element.

Synopsis

IFS(dynarray,tdyn,fdyn)

Arguments

dynarray The dynamic array tested for truth values of elements. An expression that resolves to a dynamic array of boolean value elements.
tdyn An expression that resolves to a dynamic array of replacement values. A tdyn element value replaces the corresponding dynarray element if the dynarray element has a value of True.
fdyn An expression that resolves to a dynamic array of replacement values. A fdyn element value replaces the corresponding dynarray element if the dynarray element has a value of False.

Description

The IFS function returns a dynamic array consisting of element values from the tdyn and fdyn dynamic arrays. IFS evaluates each element of dynarray as either true or false. If an element is evaluated as true, the corresponding element from tdyn is included in the returned dynamic array. If an element is evaluated as false, the corresponding element from fdyn is included in the returned dynamic array.

The IFS truth test is as follows:

  • 0 (zero) is evaluated as False.

  • 1 or any other non-zero numeric is evaluated as True.

  • a non-numeric value is evaluated as False.

  • an empty string or absent element value is evaluated as False.

IFS does not perform arithmetic evaluation of expressions. For example, the element values “7=7” and “7=8” would both evaluate as True, because they both are non-zero numerics. Similarly, “5–5” or “5*0” evaluate as True.

IFS only recognizes numeric values as logically true or false. It does not recognize “T” or “F” (or any other alphabetic string) as having a logical value. All alphabetic strings evaluate to False.

IFS must have a corresponding tdyn or fdyn element value (or, preferably, both) in order to evaluate an element.

If dynarray contains more than one element, IFS returns the number of elements in dynarray, if either tdyn or fdyn contain at least that many elements. Excess tdyn or fdyn elements are ignored. If either tdyn or fdyn does not contain enough elements, an empty string element is supplied. If both tdyn and fdyn do not contain enough elements,IFS returns a dynamic array containing the number of elements in the longer of tdyn or fdyn.

Single Elements in dynarray

If dynarray is a numeric or string value, it is treated as a dynamic array with one element. If dynarray is a single data value (True or False), the returned dynamic array consists of either all of the tdyn true values or all of the fdyn false values, depending on the true or false value of dynarray. This is shown in the following example:

booldyn=1
tdyn="1":@VM:"2":@VM:"3":@VM:"4":@VM:"5"
fdyn="missing1":@VM:"missing2":@VM:"missing3":@VM:"missing4":@VM:"missing5"
PRINT IFS(booldyn,tdyn,fdyn)

This program returns the dynamic array 1ý2ý3ý4ý5.

If dynarray contains a single field element value, with no value element or subvalue element values, the returned dynamic array element for that field consists of either all tdyn or all fdyn values for that field and all of its value and subvalue elements. This is shown in the following example:

booldyn=1:@FM:0:@FM
tdyn="1":@VM:"one":@VM:"uno":@FM:"2":@VM:"two":@VM:"due":@FM:"3":@VM:"three":@VM:"tre"
fdyn="A":@VM:"alpha":@VM:"alef":@FM:"B":@VM:"beta":@VM:"bet":@FM:"G":@VM:"gamma":@VM:"gimel"
PRINT IFS(booldyn,tdyn,fdyn)

This program returns the dynamic array 1ýoneýunoþBýbetaýbetþ3ýthreeýtre

If dynarray contains a single value element value with no subvalue element values, the returned dynamic array element for that value element consists of either all tdyn or all fdyn values for that value element and all of its subvalue elements.

Examples

The following example uses the IFS function to return a dynamic array based on truth values:

booldyn="0":@VM:"1":@VM:"1":@VM:"0":@VM:"1"
tdyn="1":@VM:"2":@VM:"3":@VM:"4":@VM:"5"
fdyn="missing1":@VM:"missing2":@VM:"missing3":@VM:"missing4":@VM:"missing5"
PRINT IFS(booldyn,tdyn,fdyn)

This program returns the dynamic array missing1ý2ý3ýmissing4ý5.

The following example shows that tdyn and fdyn may have more elements than dynarray:

booldyn="0":@VM:"1":@VM:"1"
tdyn="1":@VM:"2":@VM:"3":@VM:"4":@VM:"5"
fdyn="missing1":@VM:"missing2":@VM:"missing3":@VM:"missing4":@VM:"missing5"
PRINT IFS(booldyn,tdyn,fdyn)

This program returns the dynamic array missing1ý2ý3.

The following examples show what happens when tdyn or fdyn have fewer elements than dynarray:

booldyn="0":@VM:"1":@VM:"1":@VM:"0":@VM:"1"
tdyn="1":@VM:"2":@VM:"3":@VM:"4":@VM:"5"
fdyn="missing1":@VM:"missing2":@VM:"missing3":@VM:"missing4"
PRINT IFS(booldyn,tdyn,fdyn)

This program returns the dynamic array missing1ý2ý3ýmissing4ý. Note that the returned dynamic array does not specify a value for the fifth element.

booldyn="0":@VM:"1":@VM:"1":@VM:"0":@VM:"1"
tdyn="1":@VM:"2":@VM:"3":@VM:"4"
fdyn="missing1":@VM:"missing2":@VM:"missing3":@VM:"missing4":@VM:"missing5"
PRINT IFS(booldyn,tdyn,fdyn)

This program returns the dynamic array missing1ý2ý3ýmissing4ýmissing5.

See Also

FeedbackOpens in a new tab