Skip to main content

lastIndexOf (Function)

Function returns the index of the given value.

Syntax

<INT> list:lastIndexOf(<OBJECT> list, <OBJECT|INT|LONG|FLOAT|DOUBLE|BOOL|STRING> value)

Query Parameters

NameDescriptionDefault ValuePossible Data TypesOptionalDynamic
listThe list to be checked to get index of an element.OBJECTNoYes
valueValue for which last index needs to be identified.OBJECT INT LONG FLOAT DOUBLE BOOL STRINGNoYes

Example 1

list:lastIndexOf(stockSymbols, 'IBM')

The list:lastIndexOf(stockSymbols, 'IBM') function checks the stockSymbols list for the last occurrence of the value 'IBM'. It returns the index of the last occurrence if found; otherwise, it returns -1.

Example 2

CREATE STREAM InputStream (stockList OBJECT, symbol STRING);
CREATE SINK STREAM OutputStream (symbolIndex INT);

@info(name = 'SymbolSearch')
INSERT INTO OutputStream
SELECT list:lastIndexOf(stockList, symbol) AS symbolIndex
FROM InputStream;

In this stream worker example, a query named SymbolSearch processes events from the InputStream, which contains a list of stock symbols (stockList) and a target stock symbol (symbol). The list:lastIndexOf(stockList, symbol) function checks the stockList for the last occurrence of the target symbol. If the target symbol is found, the function outputs the index of its last occurrence to symbolIndex for each event to the OutputStream. If the target symbol is not found, the function outputs -1 to symbolIndex.