Sequence functions
Sequence functions work on sequences of values. With them, you can:
-
count the values (
count,count-duplicates); -
remove or find repeated values (
distinct-values,get-duplicates); -
combine two sequences as sets (
value-union,value-intersect,value-except); -
change the order of a sequence, or take a part of it (
sort,reverse,subsequence); -
find the position of a value (
index-of); -
build a sequence from a text (
split).
You can give a sequence in any of the usual ways: a reference to a field that can have several values from the
context, a sequence literal such as
['a', 'b'], an iteration, a variable that
holds a sequence, or the result of another sequence function. A single value is also accepted, as a sequence of one
value. All the values of a sequence have the same type. A function that returns a sequence returns a sequence of the
same type as its argument. The functions that take two sequences require both to have the same type.
Other functions and operators work on sequences too:
-
the functions sequence-equal, sum, min, max, average and string-join;
-
the operators described in Membership (
in,not in), Emptiness (is empty), Uniqueness (is unique in) and Duplicates (has duplicates); -
the index.
Several functions compare values: count-duplicates, distinct-values, get-duplicates, value-union,
value-intersect, value-except, sort and index-of. They compare texts exactly, so upper-case and lower-case
letters are different. They compare numbers by value, so 2 and 2.0 are equal.
count
Returns the number of values in a sequence, or the number of occurrences of a node.
count(<sequence>) → number count(<node-reference>) → number
- <sequence>
-
sequence of text, number, indicator, date, time or duration values.
- <node-reference>
-
a reference to a node, which can be absolute, have predicates and an index, or follow a context override.
The result is the number of values, duplicates included. An empty sequence gives 0. A field that has at most one
value from the current context gives 0 or 1, and so does any other single value. A multilingual text field has
one value for each language.
With a node reference, the result is the number of occurrences of the node found from the context.
Examples
count(BT-13-Text)
The number of values of the repeatable field BT-13-Text.
count(BT-13-Text[BT-13-Indicator == TRUE]) > 0
Counts only the values that meet a condition.
count(ND-RepeatableNode)
Counts the occurrences of a node.
| EFX-1 could not count nodes. |
See also: count-duplicates,
Emptiness (is empty),
string-length
count-duplicates
Returns the number of values in a sequence that repeat an earlier value.
count-duplicates(<sequence>) → number
- <sequence>
-
sequence of text, number, indicator, date, time or duration values.
The result is the number of values in the sequence minus the number of different values: ['a', 'b', 'a', 'a']
gives 2. A sequence without duplicates, and an empty sequence, give 0.
Examples
count-duplicates(['a', 'b', 'a', 'a'])
Gives 2.
count-duplicates(BT-13-Text) == 0
TRUE when no value of the repeatable field occurs twice. The operator has no duplicates makes the same test.
| New in EFX-2. |
See also: get-duplicates,
Duplicates (has duplicates)
distinct-values
Returns the values of a sequence without duplicates.
distinct-values(<sequence>) → sequence
- <sequence>
-
sequence of text, number, indicator, date, time or duration values.
The result contains each different value of the sequence once. The order of the values in the result is not defined. When the order matters, use sort.
Examples
distinct-values(BT-13-Text)
count(distinct-values(BT-13-Text)) == count(BT-13-Text)
TRUE when every value occurs only once.
sort(distinct-values(BT-13-Date))
The different dates, from the earliest to the latest.
get-duplicates
Returns the values that occur more than once in a sequence.
get-duplicates(<sequence>) → sequence
- <sequence>
-
sequence of text, number, indicator, date, time or duration values.
The result contains, once, each value that occurs at least twice in the sequence. A sequence without duplicates gives an empty sequence. The order of the values in the result is not defined.
Examples
get-duplicates(['a', 'b', 'a', 'c', 'b', 'a'])
Gives a and b.
string-join(get-duplicates(BT-13-Text), ', ')
Lists the repeated values, for example in a message.
| New in EFX-2. |
value-union
Returns the values that occur in either of two sequences.
value-union(<sequence-1>, <sequence-2>) → sequence
- <sequence-1>
-
sequence of text, number, indicator, date, time or duration values.
- <sequence-2>
-
sequence of values of the same type as
<sequence-1>.
The result contains each different value of the two sequences once, as in the union of two sets. The order of the values in the result is not defined.
Restrictions
-
Both sequences must have the same type. A sequence of texts with a sequence of numbers is an error.
Examples
value-union(BT-13-Text, BT-14-Text)
count(value-union(BT-13-Text, BT-14-Text))
The number of different values in the two fields together.
See also: value-intersect, value-except, distinct-values
value-intersect
Returns the values that occur in both of two sequences.
value-intersect(<sequence-1>, <sequence-2>) → sequence
- <sequence-1>
-
sequence of text, number, indicator, date, time or duration values.
- <sequence-2>
-
sequence of values of the same type as
<sequence-1>.
The result contains, once, each value of <sequence-1> that also occurs in <sequence-2>, as in the intersection of
two sets. The order of the values in the result is not defined.
Restrictions
-
Both sequences must have the same type.
Examples
value-intersect(BT-13-Text, BT-14-Text)
count(value-intersect(BT-13-Text, ['a', 'b'])) > 0
TRUE when the field holds a or b.
view-templates/X01.efx)LET indicator:?exists-common-value(text*:$seq1, text*:$seq2) = (count(value-intersect($seq1, $seq2)) > 0);
A user-defined function that tests whether two sequences have a value in common.
See also: value-union,
value-except,
Membership (in, not in)
value-except
Returns the values of a sequence that do not occur in another sequence.
value-except(<sequence-1>, <sequence-2>) → sequence
- <sequence-1>
-
sequence of text, number, indicator, date, time or duration values.
- <sequence-2>
-
sequence of values of the same type as
<sequence-1>.
The result contains, once, each value of <sequence-1> that does not occur in <sequence-2>, as in the difference of
two sets. The order of the values in the result is not defined.
Restrictions
-
Both sequences must have the same type.
Examples
value-except(BT-13-Text, BT-14-Text)
The values of BT-13-Text that BT-14-Text does not have.
value-except(BT-13-Text, ['a', 'b', 'c']) is empty
TRUE when every value of the field is a, b or c.
See also: value-union, value-intersect, sequence-equal
sort
Returns the values of a sequence in ascending order.
sort(<sequence>) → sequence
- <sequence>
-
sequence of text, number, indicator, date, time or duration values.
sort puts the values in ascending order, and keeps the duplicates. The order depends on the type:
-
Numbers: by value.
-
Texts: character by character, by the Unicode code of each character. This is not the alphabetical order of any language. Digits come before upper-case letters, upper-case letters before lower-case letters, and accented letters after
z. The text'10'comes before'9'. -
Indicators:
FALSEcomes beforeTRUE. -
Dates and times: from the earliest to the latest.
-
Durations: from the shortest to the longest.
For descending order, reverse the sorted sequence with reverse.
Restrictions
-
A sequence of durations cannot mix durations in years and months with durations in days.
sortcannot sort such a sequence, which causes an error when the expression is evaluated.
Examples
sort(BT-13-Date)[at 1]
The earliest of the dates.
reverse(sort(BT-13-Number))
The numbers from the largest to the smallest.
| New in EFX-2. |
reverse
Returns the values of a sequence in reverse order.
reverse(<sequence>) → sequence
- <sequence>
-
sequence of text, number, indicator, date, time or duration values.
The result contains the same values, from the last to the first.
Examples
reverse(BT-13-Text)
reverse(sort(BT-13-Date))[at 1]
The latest of the dates.
| New in EFX-2. |
See also: sort
subsequence
Returns a part of a sequence, given its starting position and, optionally, its length.
subsequence(<sequence>, <start>) → sequence subsequence(<sequence>, <start>, <length>) → sequence
- <sequence>
-
sequence of text, number, indicator, date, time or duration values.
- <start>
-
number. The position of the first value to keep, counting from 1.
- <length>
-
number. Optional. The number of values to keep. Without it, the part extends to the end of the sequence.
subsequence first rounds <start> and <length> to the nearest whole number, rounding a half up. The result then
contains each value whose position is at least <start> and, when you give <length>, lower than <start> plus
<length>. Positions outside the sequence do not count. So a start of 0 with a length of 2 keeps only the first
value, and a start after the end gives an empty sequence.
Examples
subsequence(BT-13-Text, 2)
All the values except the first.
subsequence(reverse(sort(BT-13-Number)), 1, 3)
The three largest numbers.
| New in EFX-2. |
index-of
Returns the position of the first occurrence of a value in a sequence.
index-of(<sequence>, <value>) → number
- <sequence>
-
sequence of text, number, indicator, date, time or duration values.
- <value>
-
a single value of the same type as the values of
<sequence>.
The result is the position, counting from 1, of the first value of <sequence> that is equal to <value>. If no
value is equal, the result has no value. Note the difference from the function
index-of-substring, which looks inside a text,
and returns 0 in that case.
Restrictions
-
<value>must have the same type as the values of the sequence. -
<value>must have a value. If it has none, for example because it is a field that is not present in the notice, an error occurs when the expression is evaluated.
Examples
index-of(['a', 'b', 'c'], 'b')
Gives 2.
index-of(BT-13-Text, BT-00-Text) == 1
TRUE when the first value of the repeatable field is equal to the value of BT-00-Text.
| New in EFX-2. |
See also: Membership (in, not in),
Index,
index-of-substring
split
Splits a text into a sequence of texts at each occurrence of a delimiter.
split(<text>, <delimiter>) → text*
- <text>
-
text. The text to split.
- <delimiter>
-
text. The text that separates the parts.
splitlooks for it exactly, character by character: no character has a special meaning.
The result is the sequence of the parts of <text> between the occurrences of <delimiter>, in order. The
delimiters are not part of the result. Two delimiters in a row give an empty text between them. A delimiter at the
start or at the end of the text gives an empty text at the start or at the end of the sequence. A text that does not
contain the delimiter gives a sequence with that text alone. An empty text, or a text without value, gives an empty
sequence.
Restrictions
-
<delimiter>cannot be an empty text. An empty delimiter causes an error when the expression is evaluated.
Examples
split('a,b,,c', ',')
Gives the four texts a, b, an empty text and c.
count(split(BT-00-Text, ';'))
The number of parts of a text separated by semicolons.
every text:$part in split(BT-00-Text, ',') satisfies $part in [...accessibility]
Checks that each part of a comma-separated text is a code of the codelist accessibility.
| New in EFX-2. |
See also: string-join, substring-before, Quantified expression