fieldCount
fieldCount, function
def pure fieldCount(haystack: text, separator: text): number
Returns the number of fields separated by the separator
within the haystack
.
haystack
: the full textseparator
: the text that separates the fields
Example
haystack = "a-b-c"
separator = "-"
n = fieldCount(haystack, separator)
table T = extend.range(n)
show table "Field Values" a1b3 with
field(haystack, separator, T.N - 1) // displays a, b, c
Errors
containsCount(…, “”): separator may not be empty.
The separator
cannot be an empty text value.
Remarks
This function is case-sensitive.
Recipes and best practices
This function should be used for a parsing usage, or to extend a table. The containsCount function should not be used in this context.
You can also extend a table at read with the “split” reading option.
table Items = with
[|as Description, as Colors |]
[|"pant", "blue,green,red" |]
[|"tshirt", "blue,orange" |]
[|"jacket", "red" |]
Items.Extend = fieldCount(Items.Colors, ",")
table ItemsExt = extend.range(Items.Extend)
ItemsExt.Color = field(Items.Colors, ",", ItemsExt.N - 1)
show table "ItemsExt" with
ItemsExt.N
Items.Description
Items.Colors
ItemsExt.Color
This code returns:
N | Description | Colors | Color |
---|---|---|---|
1 | pant | blue,green,red | blue |
2 | pant | blue,green,red | green |
3 | pant | blue,green,red | red |
1 | tshirt | blue,orange | blue |
2 | tshirt | blue,orange | orange |
1 | jacket | red | red |