PARSE instruction examples

[Autolink] Menu

Now some further examples for using the PARSE instruction. By the way: The best method to learn about PARSE is using REXXTRY (better yet, EREXXTRY) to test it. Double-Click on to call REXXTRY right now.

Further examples of using PARSE:

 
/* ----- PARSE the contents of a variable --------------------------- */

                            /* use PARSE VAR                          */
  testVariable = "1part 2part 3part"

  parse var testVariable resultStr1 resultStr2 resultStr3

        /* result:                                                    */
        /* -> resultStr1 = "1part", resultStr2 = "2part" and          */
        /*    resultStr3 = "3part"                                    */

                            /* use PARSE VALUE                        */
  testVariable = "1part 2part 3part"

  parse value testVariable WITH resultStr1 resultStr2 resultStr3

        /* result:                                                    */
        /* -> resultStr1 = "1part", resultStr2 = "2part" and          */
        /*    resultStr3 = "3part"                                    */

/* ----- PARSE the result of a function ----------------------------- */

                            /* use PARSE VALUE                        */
  parse value SysTextScreenSize() WITH cols rows

                            /* use PARSE VAR                          */
  textScreenSize = SysTextScreenSize()

  parse var textScreenSize WITH cols rows

/* ------ PARSE the date into day, month and year ------------------- */
/*   All examples assume a date in the American format (mm/dd/yy)!    */

                            /* parse with string pattern              */
  parse value date( "U" ) WITH month "/" day "/" year

        /* result:                                                    */
        /* -> day = "08", month = "01" and year = "96" if the current */
        /*    date is 08 Jan 1996                                     */

                            /* parse with string pattern, ignore the  */
                            /* month and year                         */
  parse value date( "U" ) WITH  . "/" day "/" .
                            /* or */
  parse value date( "U" ) WITH  "/" day "/"

                            /* parse with variable string pattern     */
  separator = "/"
  parse value date( "U" ) WITH month (separator) day (separator) year

                            /* parse with variable string pattern     */
                            /* The separator character is part of the */
                            /* source. It is saved in the variable    */
                            /* "separator" and used with              */
                            /* "(separator)".                         */
  parse value date( "U" ) WITH month 3 separator 4 day (separator) year

                            /* absolute positional parsing            */
  parse value date( "U" ) WITH month 3 4 day 6 7 year

                            /* absolute positional parsing with       */
                            /* changed parse order                    */
  parse value date( "U" ) WITH 7 year 1 month 3 4 day 6 .

                            /* absolute positional parsing, ignore    */
                            /* the month and year                     */
  parse value date( "U" ) WITH  4 day 6

                            /* relative positional parsing            */
  parse value date( "U" ) WITH month +2 +1 day +2 +1 year

                            /* relative positional parsing            */
                            /* with changed parse order               */
  parse value date( "U" ) WITH 4 day +2 +1 year +2 -8 month +2

                            /* relative positional parsing            */
                            /* ignore the month and year              */
  parse value date( "U" ) WITH  +3 day +2

/* ------ PARSE the date into day, month and year (all formats) ----- */
    /* dateVar may contain something like "01.04.95" (European) or    */
    /* "04/01/95" (US)                                                */

  if dateFormat = "US" then
  do
                    /* US date format is mm/dd/yy                     */
    monthPos = 1;  dayPos = 4;    yearPos = 7
  end /* if */
  else
  do
                    /* European date format is dd/mm/yy               */
    monthPos = 4;  dayPos = 1;    yearPos = 7
  end /* else */

  parse var dateVar =(dayPos) day +2 ,
                    =(monthPos) month +2 ,
                    =(yearPos) year +2

See the section Hints for PARSEing for further hints for the PARSE instruction.

Note: For a complete description of the PARSE instruction see IBM: OS/2 2.0 Procedures Language/2 Reference Manual.