4 Replies Latest reply on Feb 23, 2015 4:58 PM by AlfredoMolinolo

    Using loop in a function

    AlfredoMolinolo

      Title

      Using loop in a function

      Post

      Hi there. I have a script that adds a char string to either the right or the left side of a passed string:

      #The script accepts 4 parameters:
      #Text to be padded
      Set Variable [$String2Pad; Value:Getvalue(Get{ScriptParameter); 1)]
      #Char to use to pad
      Set Variable [$Char; Value:Getvalue(Get{ScriptParameter); 2)]
      #Lenght of the string of Char to use to pad
      Set Variable [$CharNumber; Value:GetAsNumber(Getvalue(Get{ScriptParameter); 3))]
      #Side of the passed string to which t o add the padding
      # "'L"' or no parameters is left; "'R"' is right
      Set Variable [$Side2Pad; Value:If(Getvalue(Get{ScriptParameter); 4) = "" ;0;Getvalue(Get(ScriptParameter); 4))]
      Loop
           Exit Loop If [$CharNumber = 0]
          Set Variable [$String2Pad; Value:I f($Side 2Pad = i · or $Side 2Pad = -; $Char & $String2Pad;        $String2Pad & $Char)]  

      Set Variable [$CharNumber; Value:$CharNumber - 1]

      End Loop

      The script works well, but I'd like for it to be a function, so I don't have to work with global variables or fields, but I don´t think I can loop within a function, can I?

      Thanks!

       

        • 1. Re: Using loop in a function
          SteveMartino

          You can use a custom function with recursion.  I would look up/research Custom Functions with recursion.  Someone will probably be able to help you here.

          Some more info:  What are the script parameters being passed, and can you give an actual example of what a passed string looks like, and what a character string looks like.  A small sample file with some records would help.

          • 2. Re: Using loop in a function
            AlfredoMolinolo

            Hi Steve, thanks for your answer.

            The first parameter is the string to which I want to add characters, for example: "LCH"; the second parameter is the char I want to add, e.g. "0"; the third parameter is how many repetitions of the char I want to add e.g. "5", and the fourth parameter is what side of the passed string I want to concatenate the chr string:

            if the script is called with: "LCH¶0¶5¶L" this will be the result "00000LCH"; if it is called with "LCH¶0¶5¶R", the results will be "LCH00000".

            • 3. Re: Using loop in a function
              philmodjunk

              Do you have FileMaker Advanced so that you can define this as a recursive function?

              There are websites where you can search out and find custom functions created by others and you may well find one for this already.

              //CharPad ( String ; PChar ; Ln ; Loc )
              //
              // Pad text in String with Ln number of PChar characters as either leading or trailing characters
              Case ( Ln < 1 ; String ;
                         Loc = L ; CharPad ( PChar & String ; PChar ; Ln - 1 ; Loc ) ;
                         CharPad ( String & PChar ; PChar ; Ln - 1 ; Loc )
                       )

              • 4. Re: Using loop in a function
                AlfredoMolinolo

                Thanks Phil. I'll try it; yes I have Filemaker Pro Advanced. I've been in a few places but I couldn't find a specific solution. I'll keep trying.. Thanks a lot!

                Alfredo