Skip to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Getting the Nth occurrence of a word selected in a field

Featured Replies

Hello all--

 

With a script parameter connected to a button, it's easy to determine how many times a particular word appears in a field. But how can I determine the Nth occurrence of a word or text string that's currently selected in the field?

 

In the attached screenshot, the second occurrence of the word 'text' is selected.

post-63807-0-25920600-1379525882_thumb.p

What's the best way to obtain a result of '2' when testing for this?

 

Thank you in advance.

Off the top of my head:

 

• grab the values for active selection start and size ( say, $selStart and $selSize)

 

• define your word: $yourWord = Middle ( yourText ; $selStart ; $selSize - 1 )

 

• grab the partial string from the start until the end of your word: $subString = Left ( yourText ; $selStart + $selSize - 1 )

 

• do PatternCount ( $subString ; $yourWord ); the result is n occurrences in the partial string, which is equal to the n-th occurrence in the complete string

 

Within 5 mins. probably someone will post a solution employing an esoteric function I've never heard of before …  :twitch:

I started typing this and then saw eos had posted...so I just adjusted the formula to match his explanation. The only thing I added was to make it so the function picks up the field name of the active field.  Which will make this calculation portal to any field that you want to use it.

 

Let ( [
fieldName = Evaluate ( Get ( ActiveFieldName ) ) ;
$selSize = Get ( ActiveSelectionSize ) ;
$selStart = Get ( ActiveSelectionStart ) ;
$selWord = Middle ( fieldName ; $selStart ; $selSize ) ;
$subString = Left ( fieldName ; $selStart + $selSize - 1 )
] ;
 
PatternCount ( $subString ; $selWord )
 
)

Edited by Josh Ormond
Adjusted formula.

  • Author

This is a great start, the only problem is if the word 'week' is the last word in the field, the formula will also see 'weekly', 'weeks', and 'weekend' earlier in the field and return a result of 4 instead of 1.  Is there a workaround?

 

Thanks very much.

the only problem is if the word 'week' is the last word in the field, the formula will also see 'weekly', 'weeks', and 'weekend' earlier in the field

 

Isn't that what you asked for?

 

determine the Nth occurrence of a word or text string

 

 

If you want to count the occurrences of the selected text as word, you will need a recursive custom function (or a looping script) - see:

http://www.briandunning.com/cf/1390

  • Author

Isn't that what you asked for?

 

 

 

If you want to count the occurrences of the selected text as word, you will need a recursive custom function (or a looping script) - see:

http://www.briandunning.com/cf/1390

By 'text string' I meant a contiguous selection of two or more words, such as really big fish;  I'm sorry if I used the wrong term.  I'll check out your custom function.

Hi Michael.  Always great to see your contributions!!!

 

Isn't that what you asked for?

 

 

 

If you want to count the occurrences of the selected text as word, you will need a recursive custom function (or a looping script) - see:

http://www.briandunning.com/cf/1390

  • Author

That's a nice function Michael, thanks for sharing that.  Can I make it work when more than one words are selected?  As you can see in this screenshot, the Data Viewer returns a result of 0 when three words are selected:

 

post-63807-0-25573500-1379535669_thumb.p

 

I'm hoping to see a result of 2 in this situation, as in this is the second occurrence of the string 'here's a link'.

 

I'm trying to provide a way for my users to select a word (or words) in a simple data entry field and click a button which will then insert a hyperlink in a companion 'html code' field based on the selected text. Trying to enable them to build and send an html email message in an FM layout.  Will be using 360Works' Email plugin in the process.

 

Many thanks for your time and assistance.

 

  • Author

I wish I could edit the name of this topic, I'd change it to 'Getting the Nth occurrence of a word word or phrase selected in a field'. I'm still unable to get the Nth occurrence of a 2- or 3-word phrase.

I don't have the brain power to solve your problem.  But I think you might get away with an algorithim that works two different ways.  If the user select a single word, then you use Comments method.  If the user selects multiple words then you use Josh's method.    No?

 

...what should happen if they select partial words (1/2 a word, or 2 1/2 words)?

That's a nice function Michael, thanks for sharing that.  Can I make it work when more than one words are selected?

 

I haven't tested it, but I believe it's merely* a matter of changing:

 

LeftWords ( text ; 1 )

 

to:

 

LeftWords ( text ; WordCount ( searchPhrase ) )

 

---

(*) Actually, that's not all: for best results, you should jump forward by the number of words in searchPhrase when there is a match, instead of just one when there isn't.

 

 

...what should happen if they select partial words (1/2 a word, or 2 1/2 words)?

 

Indeed; the script should probably start by expanding the selection to the edges of the selected words. Altogether, a rather ambitious undertaking - and I am not at all convinced Filemaker is the best tool for it.

Edited by comment

"I'm trying to provide a way for my users to select a word (or words) in a simple data entry field and click a button which will then insert a hyperlink in a companion 'html code' field based on the selected text."

 

I built an html "editor" a few years back and I somewhat recall that we instructed the users to add a character ( such as ~ or | ) which we would sub out for the appropriate html code. Also, solves your "partial" word dilemma. Just more explicit.

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.