5 Replies Latest reply on Aug 12, 2010 9:37 AM by philmodjunk

# Calculation loop

### Title

Calculation loop

### Post

I am trying to write a calculation that converts text in a field so that the first character of each word is capitalised. I don't want to use the Proper function as this does not allow for uppercase in the middle of words.

I have got this far:

\$Word = Upper (Left ( \$Word ; 1 ))  &  Right ( \$Word ; Length ( \$Word ) -1 )

This will set the first character of a word to uppercase. I now need to loop this for the amount of words in the field. I can get the number of words in the field using WordCount  and  retrieve a words using  LeftWords and RightWords but I need to loop this. I dont know how so set this loop:

Pseudocode

For (\$i = 1 to Wordcount ( \$Scentence ))

{

\$Word = Get Word ( \$Sentence;   \$i);

\$Word = Upper (Left ( \$Word ; 1 ))  &  Right ( \$Word ; Length ( \$Word ) -1 );

Set Word ( \$Sentence; \$i);

\$i = \$i + 1;

}

Any help appreciated

Lee

• ###### 1. Re: Calculation loop

Do you have filemaker advanced so that you can set up a custom function for this. Custom functions can be set up as recursive functions and this will enable the loop you want.

• ###### 2. Re: Calculation loop

How do I go about the recursive function?

To rebuild the the string I am assuming I have to set a new variable \$NewString then \$NewString = \$NewString & " " & \$Word which will add a leading space at the begining of the string, then Trim ( \$NewString).

Cheers

Lee

• ###### 3. Re: Calculation loop

A recursive function is a function that calls itself, passing a simpler version of the original data so that eventually a value is returned which then chains back through the previous calls until a result for the original data is produced. It has a smoke and mirrors look to many, but it works.

Let's call your function SmartCapital with a single parameter: txtWords.

Let ( Word = LeftWords ( txtText ; 1 ) ;
Case ( WordCount ( txtWords ) < 1 ; Upper ( Left ( Word ; 1 ) ) & Right ( Word ; Length ( Word ) - 1 ) ;
Upper ( Left ( Word ; 1 ) ) & Right ( Word ; Length ( Word ) - 1 ) & " " & SmartCapital ( RightWords ( txtWords ; WordCount ( txtWords ) - 1 ) )
) /* end case */
) /* end let */

You'd use this in an expression such as SmartCapital ( FieldName ) to capitalize the contents of field name. You can also use it in an auto-enter calculation such as SmartCapital ( self ) to auto capitalize text after it has been entered in a text field.

The function capitalizes the last word first, then the next to last, and so forth until a capitalized string of all the words are returned by the original call to the function.

Note: custom functions work best in unstored calculations or auto-enter calculations. They can be made to work with stored calculations, but be aware of the following issue while developing and testing a new custom function:

Calculation fields using Custom Function do not re-evalute when custom function is updated

• ###### 4. Re: Calculation loop

Thanks Phil

So the variable 'word'  is created locally to each function call. So if there are five words inthe string, five seperate 'word' variables are created local to each call of the function. The function is called five times until there is one word (WordCount( txtWords) <= 1)which is then capitalised, and concatonated to the previous words, then that function ends. Then remainder of the code in the previously called function is executed, capitalises that word then concatonated to the previous words, the function exited and so on untill all five words are capitalised then the original function exited with all words capitalised.

I takes a little to get your head around but does make sense. I hope my understanding is correct?

Again many thanks. Your help is always appreciated.

Lee

• ###### 5. Re: Calculation loop

Exactly, that's how "end point recursion--which is what this is called in the computer programming textbooks--works. And this is why I said it looks a bit like "smoke and mirrors" to many. ;-)