GetIndex ( )

Function stats

Average user rating
4.0000
104
147
9999
Support
FileMaker 7.0 +
Date posted
17 December 2008
Last updated
17 December 2008
Version
Recursive function
Yes

Author Info
 Jeroen Aarts

3 functions

Average Rating 4.0

author_avatar



 

Function overview

Prototype

GetIndex  ( Array;   Value )


Parameters

Array  Return-separated list of values


Value  Value to look for


Description

Tags:  Values   Position   List  

Finds the position (index) of Value in Array, a return-separated list of values.

Examples

Sample input

GetIndex("a¶b¶c¶c¶d¶e" ; "e")


Sample output

6

 

Function code

/*
Gets position of Value in array; Start = start index to search

- Jeroen Aarts, ClickWorks bvba, www.clickworks.be
*/

Let( [
firstVal = LeftValues ( Array ; 1 ) ;
restValues = MiddleValues ( Array ; 2 ; ValueCount ( Array ) - 1 )
];
Case(
not ValueCount ( FilterValues ( Array ; Value ) ) or not ValueCount ( Array ) ; 0 ;
firstVal = Value ; 1 ;
1 + GetIndex ( restValues ; Value )
)
)


// ===================================
/*

    This function is published on FileMaker Custom Functions
    to check for updates and provide feedback and bug reports
    please visit http://www.fmfunctions.com/fid/147

    Prototype: GetIndex( Array; Value )
    Function Author: Jeroen Aarts (http://www.fmfunctions.com/mid/104)
    Last updated: 17 December 2008
    Version: 1.2

*/
// ===================================

 

Comments

Fabrice
17 December 2008



Hi Jeroen,
just a little mistake in the documentation : it's FileMaker 8 compatible because it uses GetValue (actually you could make it FM7 compliant by using LeftValues instead)
  General comment
Daniele Raybaudi
17 December 2008



Hi Jeroen

PatternCount ( ) is not a good choice...

GetIndex ( "aa¶aa¶et ; "e" ) will give incorrect result.

I suggest:

Let( [
firstVal = LeftValues ( Array ; 1 ) ;
restValues = MiddleValues ( Array ; 2 ; ValueCount ( Array ) - 1 )
];
Case(
not ValueCount ( FilterValues ( Array ; Value ) ) or not ValueCount ( Array ) ; 0 ;
firstVal = Value ; 1 ;
1 + GetIndex ( restValues ; Value )
)
)
(Edited by Daniele Raybaudi on 17/12/08 )
  General comment
Jeroen Aarts
17 December 2008



@Fabrice:
I knew I could trust you to make sure everything is right. Thanks, I will happliy update the custom function inf :-)
  General comment
Jeroen Aarts
17 December 2008



@Daniele
You are correct, thanks for pointing that out. Also, there is no practical difference between the first two test (value not found in original, or value not found within recursion), so we may safely return 0 in both cases. Thanks again, I updated the function.
  General comment
Andries Heylen
29 March 2013



Hi Jeroen

I tried to come up with a non recursive version of this function which you can find here: http://fmcustomfunctions.com/functions_display_record.php?functionId=354

However it looks so simple I am doubting if I am not missing something...
  General comment
Jeroen Aarts
29 March 2013



Andries, good job, it does the job too! I don't see any cases where the function would return incorrect resulsts
  General comment
Kris Musshorn
10 November 2014



Jeroen,
Noticed that the CF does not handle multiple values. If array = List ( 1; 1; 2;1; 3; 4 ) and Value = 1 the CF returns 4. It's not an incorrect result but is an incomplete result which may trip up some users. I would suggest that the function could/should return List (1;2;4) in my example
  General comment
Jeroen Aarts
10 November 2014



Kris, you have a good point there. But this function does not handle multiple occurences by design. I aimed to make the function as simple as possible by design and use. A function that would return the result suggested by you would me more complex and would use tail recursion IMHO. However, I took your comment as a challenge and you can find a new function here, which returns a list of the positions of all occurrences of Value in Array: http://www.fmfunctions.com/fid/371
(Edited by Jeroen Aarts on 10/11/14 )
  General comment

 

 

 

 

 

Top Tags

Text Parsing  (33)
List  (31)
Date  (28)
XML  (26)
Format  (23)
Sql  (22)
Dev  (20)
Debug  (17)
Interface  (15)
Layout  (15)
Text  (14)
Variables  (12)
Layout Objects  (11)
Filter  (11)
Design  (10)
Array  (7)