9 Replies Latest reply on Apr 24, 2014 9:51 AM by steve_ssh

    Splitting a string into 3 vars

    chronister

      Hello all,

       

      Got a question about splitting a string. I have been searching for this and I am sure it is easy and I am hoping you gurus can help me out once again. I need to split a string into 3 variables. Each piece is seperated by a hyphen. Here are examples of the strings I need to split. This is running in a script to import data from one table to another.

       

      Gildan - Heavy Blend™ Hooded Sweatshirt - 18500
      Hanes - PrintProXP Ultimate Cotton® Hooded Sweatshirt - F170

      Gildan - DryBlend™ Hooded Sweatshirt - 12500

       

      As it loops through the records that contain these lines, I am aiming to set these as 3 variables....

       

      $brand

      $desc

      $model

       

      If there is a 1 line solution, that is great, but if it needs to be set in 3 pieces (3 set variable script steps), that is cool too.

       

      Any help is appreciated.

       

      Thanks folks,

       

      Nate

        • 1. Re: Splitting a string into 3 vars
          greg@heinie.net

          Nate,

           

          Do a search on YouTube for "text parsing in filemaker" and you will learn how this is done using the Left(function) Middle(function) or Right(function).

           

          Cheers,

          Greg

          • 2. Re: Splitting a string into 3 vars
            beverly

            Or substitute the dash for returns. This makes a value list and can use the value functions. You may need trim() as well to remove leading & trailing spaces in each value.

             

            -- sent from my iPhone4 --

            Beverly Voth

            --

            1 of 1 people found this helpful
            • 3. Re: Splitting a string into 3 vars
              chronister

              Yeah, I am still digging... I can see that those 3 are going to be used, I am trying now to discern how to incorporate the position function in there to get just what I want. I got the $brand variable taken care of... and have not tried, but I imagine that getting the $model using Right() is going to be fairly straightforward, just trying to get the middle part now.....

               

              Thanks,

               

              Nate     

              • 4. Re: Splitting a string into 3 vars
                TomHays

                Since each of your 3 parts is separated by " - ", you can convert this into a FileMaker list with Substitute().  Then use GetValue() on the list.

                 

                $theList =Substitute(original; " - "; "¶")

                $brand = GetValue($theList; 1)

                $desc = GetValue($theList; 2)

                $model = GetValue($theList; 3)

                 

                 

                If you suspect that the spaces around the hyphen are inconsistent, but the hyphen itself is still reliable, you can use something like this instead.

                 

                $theList = Substitute(original; "-"; "¶")

                $brand = Trim(GetValue($theList; 1))

                $desc = Trim(GetValue($theList; 2))

                $model = Trim(GetValue($theList; 3))

                 

                 

                -Tom

                1 of 1 people found this helpful
                • 5. Re: Splitting a string into 3 vars
                  chronister

                  The substitue option is nice and works easily, but I did not post the correct sample data as my data has things like this too ...  

                   

                   

                   

                  Gildan - Ultra Cotton™ T-Shirt - 2000

                   

                  And a LARGE majority of what will be flowing through this script will have the words T-Shirt which make the substitue command useful in many applications and I thank you for showing me that as I will tuck it away for future reference, but in this case I am going to have to go with the Left() Middle() and Right() to grab everything before the first hyphen, everything in between the first and last hyphen, and then everything after the last hyphen with trim in there for good measure ...

                   

                  I have the $brand and am close on the $model... here is what I have for that ...

                   

                  Right ( productOrderLineItems::Description;      Position(productOrderLineItems::Description;"-";1;1)  )

                   

                  It is grabbing 1 char before the last hyphen as you folks who know this program can probably see lol...

                   

                  I ultimatey need to do what Greg said and check out a tutorial on parsing strings.... I need to grasp the basic functions better and how to put them together to work for me.  I have been using PHP for several years now and am familiar with MS access pretty well but this transition to trying to get our office's new FM solution built is daunting, but at the same time there are some super smooth things that FM does, so I want to understand this stuff better becasue it will pay off in the end.

                   

                  Thanks folks... as a new member to this forum, I have to say kudos to all... it is a super helpful and not snotty / rude / childish forum like some of the other programming forums I have been part of....    So Thanks for all the help everyone !!! 

                  • 6. Re: Splitting a string into 3 vars
                    chronister

                    I have the $brand and the $model... now for that tricky Middle() function...

                     

                    I am struggling with the number of chars part.

                    Here is what I have... I added the +2 so it dropped off the hyphen and first space... sloppy I know

                     


                    Trim(
                       Middle ( productOrderLineItems::Description ;
                                    Position(productOrderLineItems::Description; " - " ; 1;1)+2;

                                    ///  Calculation needed here to determine the number of chars in between the first and last hyphens that I can't figure out....          
                       )
                    )

                    • 7. Re: Splitting a string into 3 vars
                      chronister

                      Thanks folks ... I was able to put together what I need and I now have a better understanding of the Left, Middle, Right, Position, and Pattern Count for parsing strings and splitting data.

                       

                      I am now able to add items that have been ordered into our inventory with 1 click and the script searches for data needed and creates records where needed to make this all happen smoothly.

                       

                       

                      Thank you to all.     

                      • 8. Re: Splitting a string into 3 vars
                        raybaudi

                         

                        chronister ha scritto:
                         

                         

                         

                        Gildan - Ultra Cotton™ T-Shirt - 2000

                         

                        And a LARGE majority of what will be flowing through this script will have the words T-Shirt

                        A search for " - " doesn't find the dash inserted between T and Shirt. So:

                        Trim ( Substitute ( YourText ; " - " ; ¶ ) )

                        returns:

                        Gildan

                        Ultra Cotton™ T-Shirt

                        2000

                         

                        1 of 1 people found this helpful
                        • 9. Re: Splitting a string into 3 vars
                          steve_ssh

                          Hello Nate,

                           

                          I realize that you have already solved your immediate need, but I figured it might be worth mentioning an easy-to-overlook feature of FileMaker's Position calcuation function:

                           

                          Given the following function signature:

                           

                            Position( text; searchString; start; occurrence )

                           

                           

                          To Quote From the Help Page at:  http://www.filemaker.com/13help/en/html/func_ref3.33.78.html

                           

                           

                              "A negative occurrence value causes the scan to go in the opposite direction from start."

                           

                          This is a gem of a feature which can help you easily locate that last hyphen char in your string, without having to know the total number of hyphen chars, i.e. without the need for a call to PatternCount.  I'll include sample calculations below to help illustrate.

                           

                          HTH & Sincerely,

                           

                          -steve

                           

                           

                           

                          Get The Last Sub-String:

                           

                          Let([

                           

                            varFullString = productOrderLineItems::Description;

                           

                            DELIMTER = "-";

                           

                            varSourceStringLength = Length( varFullString );

                           

                                // Note use of -1 in last param, and also using full string length in second-to-last-param

                           

                            varDelimIndex = Position( varFullString; DELIMTER; varSourceStringLength; -1 );

                           

                            varCharCount =

                           

                                Case(  // Weed out case where no delimiter exists

                           

                                    varDelimIndex = 0; 0;

                           

                                    varSourceStringLength - varDelimIndex

                                )

                          ];

                           

                            Right( varFullString; varCharCount )

                          )

                           

                           

                           

                           

                          Get The Middle Sub-String:

                           

                          Let([

                           

                            varFullString = productOrderLineItems::Description;

                           

                            DELIMTER = "-";

                           

                            varSourceStringLength = Length( varFullString );

                           

                            varDelimIndex01 = Position( varFullString; DELIMTER; 0; 1 );

                           

                                // Again, note use of -1 in last param, and also using full string length in second-to-last-param

                           

                            varDelimIndex02 = Position( varFullString; DELIMTER; varSourceStringLength; -1 );

                           

                            varSubStringStart = varDelimIndex01 + 1;


                            varCharCount =

                           

                               Case(   // Weed out cases where fewer than two delimiters exist

                           

                                  varDelimIndex01 = varDelimIndex02; 0;

                           

                                  varDelimIndex02 - varSubStringStart

                              )

                          ];

                           

                            Middle( varFullString; varSubStringStart; varCharCount )

                          )