This is the legacy 4D documentation web site. Documentations are progressively being moved to developer.4d.com

Home

 
4D v19.8
collection.lastIndexOf( )

collection.lastIndexOf( ) 


 

collection.lastIndexOf ( toSearch {; startFrom} ) -> Function result 
Parameter Type   Description
toSearch  Expression in The element that is to be searched for within the collection
startFrom  Longint in Index to start the search at
Function result  Longint in Index of last occurrence of toSearch in the collection, -1 if not found

The collection.lastIndexOf( ) method searches the toSearch expression among collection elements and returns the index of the last occurrence, or -1 if it was not found.

Note: This method does not modify the original collection.

In toSearch, pass the expression to find in the collection. You can pass:

  • a scalar value (text, number, boolean, date),
  • the null value,
  • an object or a collection reference.

toSearch must match exactly the element to find (the same rules as for the equality operator are applied, see Comparison Operators).

Optionally, you can pass the index of collection from which to start a reverse search in startFrom.

  • If startFrom >= the collection's length minus one (coll.length-1), the whole collection is searched (default).
  • If startFrom < 0, it is recalculated as startFrom:=startFrom+length (it is considered as the offset from the end of the collection). If the calculated value is negative, -1 is returned (the collection is not searched).
    Note: Even if startFrom is negative, the collection is still searched from right to left.
  • If startFrom = 0, -1 is returned, which means the collection is not searched.

Example  

 C_COLLECTION($col)
 $col:=Split string("a,b,c,d,e,f,g,h,i,j,e,k,e";",") //$col.length=13
 $pos1:=$col.lastIndexOf("e") // returns 12
 $pos2:=$col.lastIndexOf("e";6) //returns 4
 $pos3:=$col.lastIndexOf("e";15) // returns 12
 $pos4:=$col.lastIndexOf("e";-2) // returns 10
 $pos5:=$col.lastIndexOf("x") //returns -1



See also 

collection.indexOf( )

 
PROPERTIES 

Product: 4D
Theme: Collections
Number: 705247

This command can be run in preemptive processes

 
PAGE CONTENTS 
 
HISTORY 

Created: 4D v16 R6

 
ARTICLE USAGE

4D Language Reference ( 4D v19)
4D Language Reference ( 4D v19.1)
4D Language Reference ( 4D v19.4)
4D Language Reference ( 4D v19.5)
4D Language Reference ( 4D v19.6)
4D Language Reference ( 4D v19.7)
4D Language Reference ( 4D v19.8)