PositionInIndex


PositionInIndex(a, x, i)

Returns the position in index i -- that is, a number from 1 to the size of index i -- of the last element of array a equal to x; if no element is equal, it returns 0. For example,

Index I := ['A', 'B', 'C']
Variable A := Array(I, [1, 2, 2])
PositionInIndex(A, 1, I) --> 1
PositionInIndex(A, 2, I) --> 3  
PositionInIndex(A, 5, I) --> 0

Parameter a is optional. When omitted, it returns the position of x in the index i, or 0 if not found. For example,

PositionInIndex(, 'C', I) --> 3  
PositionInIndex(,'D', I) --> 0

The syntax @[i=x] does the same thing:

@[I = 'B'] --> 2
@[I = 'D'] --> 0

'PositionInIndex is the positional equivalent of SubIndex. You may need PositionInIndex when I contains duplicate values, in which case SubIndex isn't sufficient.

Declaration

PositionInIndex(A:optional Array[I] ; U : atomic ; I : IndexType )

Details

When A is a self-indexed table, PositionInIndex(A,u,A) and PositionInIndex( U:u, I:A ) are not the same. PositionInIndex(A,u,A) will find the value in the array A, while PositionInIndex( U:u, I:A ) finds u along the index value of A.

When I is a normal index (not a self-indexed table, and not Time within a dynamic loop), then PositionInIndex(I,u,I) and PositionInIndex( U:u, I:I ) are functionally equivalent. However, PositionInIndex( U:u, I:I ) has a speed advantage, particularly when u is an array, or when many lookups along I will be performed. The average lookup time is O(1) for PositionInIndex(U:u,I:I) but O(Size(I)) for PositionInIndex(I,u,I). @[I=u] is also O(1).


See Also

Comments


You are not allowed to post comments.