Difference between revisions of "PositionInIndex"

Line 23: Line 23:
 
== Details ==
 
== Details ==
  
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 «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).  [[Index Position Operator::@|@[i=x] ]] is also O(1).
+
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).  [[Index Position Operator::@|@[i = x] ]] is also O(1).
  
 
== Examples ==
 
== Examples ==
Line 61: Line 61:
  
 
Then:
 
Then:
  PositionInIndex(A,2,I) → 3
+
  PositionInIndex(A, 2, I) → 3
  PositionInIndex(A,3,I) → 5
+
  PositionInIndex(A, 3, I) → 5
  PositionInIndex(A,6,I) → 0
+
  PositionInIndex(A, 6, I) → 0
  PositionInIndex(A,1..5,I) → [6, 3, 5, 1, 4]
+
  PositionInIndex(A, 1..5, I) → [6, 3, 5, 1, 4]
  PositionInIndex( ,'c', I ) → 3
+
  PositionInIndex( , 'c', I ) → 3
  @[I='d'] → 4
+
  @[I = 'd'] → 4
  @[I='g'] → 0
+
  @[I = 'g'] → 0
  PositionInIndex(A,C,I) →
+
  PositionInIndex(A, C, I) →
 
{| class="wikitable"
 
{| class="wikitable"
 
! colspan="5" |K ▶
 
! colspan="5" |K ▶
Line 84: Line 84:
 
|5
 
|5
 
|}
 
|}
  PositionInIndex(B,2,I) →
+
  PositionInIndex(B, 2, I) →
 
{| class="wikitable"
 
{| class="wikitable"
 
! colspan="2" |J ▶
 
! colspan="2" |J ▶
Line 96: Line 96:
  
 
When the array is multidimensional:
 
When the array is multidimensional:
  PositionInIndex(B,C,I) →
+
  PositionInIndex(B, C, I) →
 
{| class="wikitable"
 
{| class="wikitable"
 
! colspan="2" rowspan="2" | B
 
! colspan="2" rowspan="2" | B
Line 175: Line 175:
 
  PositionInIndex(, 'D', I) → 0
 
  PositionInIndex(, 'D', I) → 0
  
The syntax @[i=x] does the same thing:
+
The syntax @[i = x] does the same thing:
 
  @[I = 'B'] → 2
 
  @[I = 'B'] → 2
 
  @[I = 'D'] → 0
 
  @[I = 'D'] → 0

Revision as of 06:36, 7 December 2015


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. When array «a» is multidimensional, the result is reduced by one dimension, dimension «i».

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

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.

Declaration

PositionInIndex(a: optional Array[I]; x: atomic; i: IndexType)

Details

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).

Examples

Let:

I → 'a' 'b' 'c' 'd' 'e' 'f'
A → 4 3 2 5 3 1
B I
'a' 'b' 'c' 'd' 'e' 'f'
J 1 3 5 2 4 5 3
2 2 1 5 2 3 4

     

K → 1 2 3 4 5
C → 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) →
B K
1 2 3 4 5
J 1 4 3 5 0 6
2 6 4 3 4 5

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

Optional Parameters

a

Parameter «a» is optional. When omitted, it returns the position of «x» 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

See Also

Comments


You are not allowed to post comments.