indexOf
Returns the first index at which a given value is found.
The first parameter takes input data (e.g. a string or list), and the second takes a value to search for. By default, the function searches forward from index 0, but you can change the direction and start index.
The function is case-sensitive.
The function returns -1 if the given value is not found.
Parameters
STRING, LIST OR TABLE (string, list, or table region)
The string, list, or table region to search.
SUBSTRING OR ITEM (string or item)
The value to search for.
OFFSET (number)
Optional. The start index for searching.
Default: 0 if SEARCH DIRECTION = forward; the last index if SEARCH DIRECTION = backward.
SEARCH DIRECTION (string)
Optional. The search direction. Specify forward or backward.
Default: forward
Examples
String examples
By default, the function searches forward from index 0.
ATL in Script | Result |
---|---|
| 0 |
| 3 |
| -1 |
| -1 |
Use the third parameter to change the start index.
ATL in Script | Result |
---|---|
| 5 |
| -1 |
Use the fourth parameter to change the search direction.
ATL in Script | Result |
---|---|
| 5 |
| 0 |
List examples
ATL in Script | Result |
---|---|
[[ myList = ('John', 'Paul', 'George', 'Ringo', 'Peter', 'Paul', 'Mary') indexOf(myList, 'Paul') ]] | 1 |
[[ myList = ('John', 'Paul', 'George', 'Ringo', 'Peter', 'Paul', 'Mary') indexOf(myList, 'P') ]] | -1 |
The last example returns -1 because the input is a list of strings and none of the strings are 'P'.
Table region examples
Assume a "Describe the Table" project with this data:
Code | Branch | Sales | |
---|---|---|---|
Row 1 | 1001 | Boston | 2,598,248.36 |
Row 2 | 1002 | Chicago | 2,267,321.77 |
Row 3 | 1003 | Denver | 2,482,434.78 |
Row 4 | 1004 | Miami | 3,175,639.12 |
Here is how you could use indexOf
in a user-defined function (UDF):
#value salesRanking(branchName)
[[
sortedBySales = sortByColumnNumbersReverse(WholeTable, 'Sales')
branchesBySales = columnsInRegion(sortedBySales, Branch)
rank = indexOf(branchesBySales, branchName) + 1
joinStrings(branchName, ' ranked ', numToWords(rank, 'ordinal'), ' for sales.')
]]
The function takes one parameter (branchName
). The first line of ATL sorts the table by sales (descending), the second line generates a list of branches (now sorted by sales), and the third finds the index for the input branch name (e.g. 'Denver') in that sorted list. The + 1
part accounts for zero-based indexing. The final line joins some fixed text and variables to form an output string.
The function is a UDF, so you need to call it another script. For example:
ATL in Script | Output Text |
---|---|
| Denver ranked third for sales. |
| Miami ranked first for sales. |