fieldr
fieldr, function
def const pure fieldr(haystack: text, separator: text, n: number): text
Returns the n
th field, zero-indexed from the right, in a text value haystack
that contains multiple sub-text values separated by a specified separator
.
This function is intended to facilitate parsing values that have been concatenated within a single table column.
Example
haystack = "a-b-c-d"
show summary "Example 1" with
fieldr(haystack, "-", 0) // "d"
fieldr(haystack, "-", 1) // "c"
fieldr(haystack, "-", 2) // "b"
Remarks
Repeated separator
If the separator
is repeated twice in a row in the haystack
, an empty field is considered to separate them:
haystack2 = "a--c-d"
show summary "Example 2" with
fieldr(haystack2, "-", 0) // "d"
fieldr(haystack2, "-", 1) // "c"
fieldr(haystack2, "-", 2) // ""
Out of range index
If the haystack
does not contain enough fields, an empty text is returned:
haystack3 = "a"
show summary "Example 3" with
fieldr(haystack3, "-", 0) // "a"
fieldr(haystack3, "-", 1) // ""
fieldr(haystack3, "-", 2) // ""
Edge cases
If the separator
is empty, or if n
is negative, the function returns an empty text :
show summary "Edge cases" with
fieldr("ab-cd", "", 1) // ""
fieldr("ab-cd", "-", -1) // ""