# Filter by length and case

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

## Recommended Posts

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

##### Share on other sites

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
##### Share on other sites

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

##### Share on other sites

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
##### Share on other sites

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?

##### Share on other sites

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

How does it know to exit the recursion?

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

##### Share on other sites

Ah yes. Thanks comment!

##### Share on other sites

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

## Create an account

Register a new account