# PositionInIndex

Release: 4.6  •  5.0  •  5.1  •  5.2  •  5.3  •  5.4  •  6.0  •  6.1  •  6.2  •  6.3

## PositionInIndex(a, u, i, first)

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 «u» (or of the first element if «first» is specified as true). If no element is equal, it returns 0. When array «a» is multidimensional, the result is reduced by one dimension, dimension «i».

(New to Analytica 5.2) You can specify whether you want the first or last occurrence of «u» by setting the optional «first» parameter to true or false. By default, the last occurrence is found.

The formal declaration of the function is PositionInIndex(a: optional Array[I]; u: atom; i: IndexType; first:optional boolean atom).

PositionInIndex is the positional equivalent of SubIndex. You may need PositionInIndex when «i» contains duplicate values, in which case SubIndex isn't sufficient because it returns an ambiguous result.

PositionInIndex is essentially the inverse of the Slice function. While Slice maps from an index position to the corresponding array value, PositionInIndex maps from the array value to the index position.

## Example

Let:

Index I := ['A', 'B', 'C']
Variable A := Array(I, [1, 2, 2])

Then:

PositionInIndex(A, 1, I) → 1
PositionInIndex(A, 2, I) → 3
PositionInIndex(A, 5, I) → 0

## Optional Parameters

### a

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

PositionInIndex(, 'B', I) → 2
PositionInIndex(, 'D', I) → 0

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

@[I = 'B'] → 2
@[I = 'D'] → 0

### first

(new to Analytica 5.2) Parameter «first» is optional. When omitted, finds the last occurrence of «u» along index «i». When set to true, returns the first occurrence along «i» (or 0 if not found).

## Details & More Examples

When «a» is a self-indexed table, PositionInIndex(a, x, a) and PositionInIndex(U: x, I: a) are not the same. PositionInIndex](a, x, a) will find the value in the array «a», while PositionInIndex](U: x, 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, x, i) and PositionInIndex(U: x, I: i) are functionally equivalent. However, PositionInIndex(U: x, I: I) has a speed advantage, particularly when «x» is an array, or when many lookups along «i» will be performed. The average lookup time is O(1) for PositionInIndex(U: x, I: i) but O(Size(i)) for PositionInIndex(i, x, i). @[i = x] is also O(1).

### Example 1

Let:

Variable A :=
I ▶
'a' 'b' 'c' 'd' 'e' 'f'
4 3 2 5 3 1
Variable B :=
I ▶
J ▼ 'a' 'b' 'c' 'd' 'e' 'f'
1 3 5 2 4 5 3
2 2 1 5 2 3 4
Variable C :=
K ▶
1 2 3 4 5
4 2 5 1 3

Then:

PositionInIndex(A, 2, I) → 3
PositionInIndex(A, 3, I) → 5
PositionInIndex(A, 6, I) → 0
PositionInIndex(A, 1..5, I) → [6, 3, 5, 1, 4]
PositionInIndex( , 'c', I ) → 3
@[I = 'd'] → 4
@[I = 'g'] → 0
PositionInIndex(A, C, I) →
K ▶
1 2 3 4 5
1 3 4 6 5
PositionInIndex(B, 2, I) →
J ▶
1 2
3 4

When the array is multidimensional:

PositionInIndex(B, C, I) →
K ▶
J ▼ 1 2 3 4 5
1 4 3 5 0 6
2 6 4 3 4 5

### Example 2

The following example can be found in the User Guide Examples.

Let:

Variable Car_prices :=
Years ▶
Car_type ▼ 2005 2006 2007 2008 2009
VW $16,000$17,000 $18,000$19,000 $20,000 Honda$18,000 $19,000$20,000 $22,000$24,000
BMW $25,000$26,000 $28,000$30,000 \$32,000
Index Car_type := ['VW', 'Honda', 'BMW']

Then:

PositionInIndex(Car_prices, 18K, Car_type) →
Years ▶
2005 2006 2007 2008 2009
2 0 1 0 0