September 18, 201312 yr 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. What's the best way to obtain a result of '2' when testing for this? Â Thank you in advance.
September 18, 201312 yr 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 …
September 18, 201312 yr 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 September 18, 201312 yr by Josh Ormond Adjusted formula.
September 18, 201312 yr 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.
September 18, 201312 yr 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
September 18, 201312 yr 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.
September 18, 201312 yr 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
September 18, 201312 yr 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: Â Â 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. Â
September 19, 201312 yr 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.
September 19, 201312 yr 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)?
September 19, 201312 yr 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 September 19, 201312 yr by comment
September 19, 201312 yr "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