Brian Dunning's FileMaker Custom Functions

WordPosition ( text ; searchWord ; start ; occurrence )

Returns the word number of the searchWord in the text.

  Average rating: 3.4 (67 votes) Log in to vote

Daniele Raybaudi   Daniele Raybaudi - Show more from this author
ACI
http://www.aci.it

Share on Facebook Share on Twitter

  Sample input:
WordPosition ( "this may be or may be not the best way" ; "be" ; 1 ; 2 )
  Sample output:
6

  Function definition: (Copy & paste into FileMaker's Edit Custom Function window)

WordPosition custom function

Author
Daniele Raybaudi

Type
Recursive

Format
WordPosition ( text ; searchWord ; start ; occurrence )

Parameters
text - any text field, or an expression that returns a reference to a text field.
searchWord - the word to search in the text
start - the word number that you want to start from
occurrence - the occurrence of the word that you want to find

Data type returned
number

Description
Returns the word number of the searchWord in the text.
In the same way that the Position function provides the number of a character position in a text string, WordPosition calculates the word number of a text string.

 

Comments

Lizhe Xu   Lizhe Xu, East Lyme, CT
Aug 26, 2009
The script does not work properly when the third parameter>1. Take the example above:
WordPosition ( "this may be or may be not the best way" ; "be" ; 2;1 )
return 4;
When the third parameter is 3, 4, 5, ... the result is 5, 6, 7,...

It seems to me there are probably two solutions for it (1) write a subCF, which is recursive and takes 5 parameters: the fifth will be constant number equal to the initial third parameter; (2) the easy solution, delete the third parameter and make it default value 1.
 
Lizhe Xu   Lizhe Xu, East Lyme, CT
Aug 26, 2009
After second thought, the second solution indeed is not so easy, it is still need a subCF, which will accept 4 parameters. I now use your CF in my solution but with the constant value 1 for the third parameter.
 
Max Heller   Max Heller, fmax software
Apr 13, 2010
I have fixed the problem Lizhe brought up, and I also added support for a negative value for occurrence.

Let(
[
count = WordCount( text );
direction = Sign( occurrence );
startWord = MiddleWords( text; start; 1 )
];
Case(
start < 1 or start > count;
0;
IsEmpty( text ) or IsEmpty( searchWord ) or IsEmpty( start ) or IsEmpty( occurrence );
"";
startWord ≠ searchWord;
WordPosition( text; searchWord; start + direction; occurrence );
startWord = searchWord and Abs( occurrence ) ≠ 1;
WordPosition( text; searchWord; start + direction; occurrence - direction );
start
)
)
 
Claudio Cuevas Merino   Claudio Cuevas Merino, Santiago Chile
Feb 8, 2017
Thanks you very much for sharing this!, works great!
 

Log in to post comments.

 

Note: these functions are not guaranteed or supported by BrianDunning.com. Please contact the individual developer with any questions or problems.

Support this website.

This library has been a free commmunity resource for FileMaker users and developers for 20 years. It receives no funding and has no advertisements. If it has helped you out, I'd really appreciate it if you could contribute whatever you think it's worth: