Jump to content

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

Recommended Posts

Posted

Hi all

I've been searching high and low for a custom function but to no avail. 

Is there a way to search a text field for all words of a particular length and case? e.g. 5 characters and upper case.

Thanks

 

Posted (edited)
  On 8/31/2015 at 10:24 PM, sal88 said:

Is there a way to search a text field for all words of a particular length and case? e.g. 5 characters and upper case.

If by "search" you mean "extract", try something like:

ExtractWords ( text ) =

Let ( [
word = LeftWords ( text ; 1 ) ;
countWords = WordCount ( text )
] ;
Case ( Length ( word ) = 5 and Exact ( word ; Upper ( word ) ) ; word &  )
&
Case ( 
countWords > 1 ; 
ExtractWords ( RightWords ( text ; countWords - 1 )  )
)
)

Given an input of:

Alpha BRAVO Charlie DELTA ECHO Foxtrot

the result will be:

BRAVO
DELTA

i.e. a return-separated list of all words in text that are exactly 5 characters long and all in uppercase.

 

 

Edited by comment
Posted

How do you do that?! Lol, I'm going to spend some time deconstructing that...I can never seem to get my head around these fancy custom functions.

Thanks so much comment, incredibly helpful as per usual :)

 

Posted (edited)
  On 9/1/2015 at 10:15 AM, sal88 said:

I can never seem to get my head around these fancy custom functions.

There's nothing "fancy" here: you want to go over each word in the given text, so you start with the first word, process it, and then call the function again with the rest of the words - so that the second word becomes the first one on the next call and so on, until you run out of words. You'd do well by spending some time "deconstructing" this, because this is the most common algorithm for a recursive custom function.

 

 

Edited by comment
Posted

OK I think I've got my head around it. This is a recursive function right? That's probably the aspect that confuses me.

Would you be able to tell me if I've got the right idea:

Let ( [
word = LeftWords ( text ; 1 ) ;
countWords = WordCount ( text )
] ;

//If the first word of 'text' is 5 characters and Upper case, then display that word and a carriage break
Case ( Length ( word ) = 5 and Exact ( word ; Upper ( word ) ) ; word & ¶ )
&
//If 'text' has more than one word, then apply this function again but with the first word trimmed off. 'text', after the initial run through, is defined recursively, each iteration trimming off the first word
Case ( 
countWords > 1 ; 
ExtractWords ( RightWords ( text ; countWords - 1 ))))


So in a sense, the function is only actually pulling in the first word (if the criteria is met) of the 'text' parameter. The part after the ampersand is only redefining the 'text' parameter?

How does it know to exit the recursion? 

Posted

Yes, that is a correct perception of how it works.

  On 9/4/2015 at 11:36 AM, sal88 said:

How does it know to exit the recursion? 

The recursive call is conditional: it only happens when countWords > 1 .

This topic is 3529 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.