7 Replies Latest reply on Aug 4, 2009 11:26 AM by ACS LLC

    Splitting up a variable  into two parts

    ACS LLC Level 1

      I've done this before but seem to be drawing a blank.


      I have a variable that has two values in it, I need to split it up so I get both values






      I need to extra the 123, and the 456


      Can anybody remind me of the easy way to do this in CF





        • 1. Re: Splitting up a variable  into two parts
          Michael Borbor Level 4

          This is one way.



          • 2. Re: Splitting up a variable  into two parts
            ACS LLC Level 1

            your code broke up to just CFLOOP, but I remember there was a command, I think it was one of the list commands?


            Just found it.. I knew it was simple! LISTFIRST and LISTLAST.. perfect when there are only two values in the string!

            • 3. Re: Splitting up a variable  into two parts
              Michael Borbor Level 4

              You can use the function ListGetAt(var, position) also.

              • 4. Re: Splitting up a variable  into two parts
                TLC-IT Level 3

                Usually, I advocate the use of regular expressions.  Yeah, it's a bit esoteric the first time you encounter it, but it's actually pretty straightforward and extremely powerful.


                Let me first refer you to the very-excellent description that will be found in Using Regular Expressions in Functions, in the ColdFusion Developer's Guide which of course can be found here:  http://livedocs.adobe.com/coldfusion/8/htmldocs/.


                Now let me try to briefly "set the stage ..."


                A regular-expression, briefly, is a string-matching pattern.  Let's say that you want to get "123," (with the comma), and "456") given the string "123,456."


                First, the (caution: extemporaneous coding!) answer: 


                parts = REMatchNoCase("([0-9]+\,)([0-9]+)", myString);




                Let's look at this string-literal, which is the pattern to be matched.  First of all, you observe that it contains two parenthesized parts, back-to-back.  (I used bold-face to make it obvious.)  The parentheses mean, in each case, that we want to extract the substring that is found to match the enclosed pattern.  We want to know where they start in the string and how long they are.  So this will give us, on a silver platter as it were, the two strings we're looking for...  or a reliable indication that they're not there.


                • Within each string you will observe "[0-9]+".  This pattern means:  "one or more" (that is, "+") "digits" (that is, "[0-9]").
                • The first group also contains "\,"  The backslash means that the next character, the comma, is to be taken as a literal character.  So the first group is matching "one-or-more digits immediately followed by a literal comma."


                The regular-expression engine is a highly optimized built-in piece of code that uses a pattern as a non-procedural declaration of what you are looking for in a character string, and it will very quickly and very reliably find it.


                There are considerable other features that we need not discuss here.  Suffice it to say that "this Swiss Army(R) Knife" is full of blades.  And bottle-openers and corkscrews and a little tool that I have no idea what it's for but it's making a peculiar buzzing noise..."


                Although you might be scared-off by "chicken scratches" at first, you will find yourself using it constantly once you understand how it works.  Just include liberal comments into your code so that you'll remember how it works, and of course, test it well.  The overwhelming advantage here is that "there is no code."  Nary a <CFIF> or a substring-search in sight.  Once you master the art of "sight-reading" a regular expression pattern, you can by inspection know that the string will work properly for anything that you might throw at it.


                Note:  The regular-expression syntax used by CF is fairly standard.  There are web-sites which will allow you to test your patterns on-line.  There are also on-line libraries of patterns, free for the taking.  This is a very fundamental skill that is well worth mastering.

                • 5. Re: Splitting up a variable  into two parts
                  ACS LLC Level 1

                  Wow, I could program the lunar module with that command! LOL


                  I think I'll stick with the listfirst, listlast for this particular case.


                  However, thanks for that very in-depth answer and tutorial, I will go back and read it several times until I get it because I know that the REGEX can be vaery powerful and useful, I've had help putting them together in the past, but never really 'got them'





                  • 6. Re: Splitting up a variable  into two parts
                    TLC-IT Level 3

                    Herein lies the subtle difference between "what works now" and "what works in the general case."


                    For example:  does this requirements-change make you sweat?


                    "Well, the pieces could actually be separated by a comma or a semicolon or a vertical bar.  Hope that's not a problem ..."


                    "Uh.  No... no it isn't... not a problem..." 


                    Sweat equals:  the approach that you had been taken happened to work, but now the requirements changed as they always do.


                    No-Sweat equals:  the approach that you took is one that applies to the general case, and that therefore can be modified as-needed without breaking.


                    For instance, had you used regexes from the start:


                    parts = REMatchNoCase("([0-9]+[,;])([0-9]+)", myString);


                    The addition is the bold-faced clause:  "[,;]" which matches any one of a set-of characters."  Now, either a comma or a semicolon in that position will be accepted.  No other changes are necessary.  You're ready to quickly handle almost every formatting-change the "garbage-in producers" might have to throw at you.  Your code is not only "more reliable now," but it is much more well-prepared to stay that way.

                    • 7. Re: Splitting up a variable  into two parts
                      ACS LLC Level 1

                      understood .. I WILL learn how to do this, I am sure once it's understood that it won't be too difficult.


                      For this particular case *I* control the creation of the string that it's receiving from another page, so I'm always guaranteed to get what I need


                      Thanks again for all your time take to explain, truely apppreciated... I hope to pass the quiz , but give me a few weeks! ;-)