-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.in_list.fmfn
49 lines (35 loc) · 1.32 KB
/
list.in_list.fmfn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
Purpose: Will return the index of the matching value (needle) in the list (haystack).
Returns: Text
Name: list.in_list ( needle; haystack )
Parameters: ( needle ) text = string to search for in the provided haystack
( haystack ) list = FileMaker List ( return delimited list )
Dependencies: NONE
References: Modeled after PHP's array_search: http://us1.php.net/manual/en/function.array-search.php
2013-12-10, JPS, Created.
*/
Let (
[
// Make sure the value is in the list
~haystackIndexLength = ValueCount ( haystack );
~needleInHaystack = not IsEmpty ( FilterValues ( haystack ; needle ));
~matchStartOfList = GetValue ( haystack ; 1 ) = needle;
~matchEndOfList = GetValue ( haystack ; ~haystackIndexLength ) = needle;
~matchInsideOfListPosition = Position ( haystack ; ¶ & needle & ¶ ; 1 ; 1 );
~matchInsideOfListIndex = PatternCount ( Left ( haystack; ~matchInsideOfListPosition ); "¶" )
];
Case (
// No matching value
not ~needleInHaystack;
0;
// Match: First Value
~matchStartOfList;
1;
// Match: Inside Value
~matchInsideOfListIndex;
~matchInsideOfListIndex + 1;
// Match: Last Value
~matchEndOfList;
~haystackIndexLength
)
)