2 Replies Latest reply on Jul 20, 2011 2:07 PM by raybaudi

    Re: How to create a custom function using tail recursion technique

    micromd

      Title

      Re: How to create a custom function using tail recursion technique

      Post

      I have a registration form with about 20 fixed filling areas (with empty filling squares in each filling area) and want to fill in each empty square with one character from my database information, e.g. a name like "JACKIE" and the form has only 4 spaces to fill in and I have to truncate the word "JACKIE" to "JACK" and the filling squares are large. I need to pad the word "JACK" with spaces like "J A C K" to fill in the form.

      I can easily create a generic script to truncate the word "JACKIE" to "JACK" and separate the characters with spaces with a loop script step.

      I read about the tail recursion but could not understand how to create a custom function with a recursive loop.

      I wonder if any FM experts can help me to create a custom function e.g. fnPaddedWord (word; characters; spaces) so that all I need is to provide (1) the word to fill in (i.e. JACKIE), (2) the number of characters to fill in (i.e. 4) & (3) fixed amount of spaces to separate the characters to be able to fill in the required area as "J A C K"

        • 1. Re: Re: How to create a custom function using tail recursion technique
          philmodjunk

          Recursive Calculations is one of the main reasons for devising a custom function.

          // fnPaddedWord ( Word , Characters , Spaces )
          // Word: the text to be padded
          // Characters the number of characters from word to be padded. Remaining characters will be truncated
          // Spaces: the number of spaces to insert between each nontruncated character

          Case ( characters = 0 ; "" ;
                    IsEmpty ( Word ) ; "" ;
                    Left ( Word ; 1 ) & Substitute ( 10^Spaces  - 1 ; 9 ; " " ) & Trim ( fnPaddedWord ( Right ( Word ; Length ( Word ) - 1 ; Characters - 1 ; Spaces ) )
                  )

          How it works: 10^Spaces is 10 raised to the Spaces power. If Spaces = 2, this produces 100. Subtracting 1 produces a number with only the 9 digit and one such digit for every space (99 when spaces = 2, 999 when spaces = 3 and so forth). The substitute function replaces each 9 with a space to pad the characters.

          The function has two conditions that will terminate the expression: If there are no more characters to Pad, or if the specified number of characters has been padded.

          Also, since the function returns spaces after the last padded character, Trim is used to remove the trailing spaces.

          • 2. Re: Re: How to create a custom function using tail recursion technique
            raybaudi

            ... or you could use calculated repeating fields, so you will be able to center the chars into their respective boxes.

            For example:

            Middle ( Extend ( word ) ; Get ( CalculationRepeatitionNumber ) ; 1 )