Jump to content
Claris Engage 2025 - March 25-26 Austin Texas ×

This topic is 4082 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Posted

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.

Posted

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:

  • Like 1
Posted (edited)

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

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.

Posted

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

  • Like 1
Posted

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.

Posted

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

Posted

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.

 

Posted

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.

Posted

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

Posted (edited)

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
Posted

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

This topic is 4082 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

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