[comp.lang.rexx] looking at individual characters in a string

waynet@wiffle.techbook.com (Wayne Tilton) (02/10/91)

>   What I am looking for is a way to look at the individual characters
>that are contained in a Rexx string.  Suppose I do :
>
>                parse pull mystring
>
>Then I want to see individual characters in mystring.  I know I could
>use the index() function or substr(), but that seems wasteful, not to
>mention slow.

You can do this with the PARSE instruction, but the code tends to get
long if you have lots of characters.  The following example will put
the first 10 characters into individual stem vars as well as the
entire string into the var 'mystring'.  It is _MUCH_ faster than using
repeated calls to SUBSTR but is not as flexible.

    parse pull 1 c.1 2 c.2 3 c.3 4 c.4 5 c.5 6 c.6 7 c.7 8 c.8 ,
      9 c.9 10 c.10 11 1 mystring

Note that instruction length limitations in various REXX implementations
(~500 chars in VM/CMS I think) limit the number of vars that can be
assigned this way.

If the string was long, a similar technique could be used in a loop
to slice it up in more manageable chunks (still faster then SUBSTR):

   parse pull mystring 1 workstr
   do forever
      parse var workstr 1 c.1 2 c.2 3 c.3 4 c.4 5 c.5 6 c.6 7,
         c.7 8 c.8 9 c.9 10 c.10 11 workstr
      /*...*/
      /* insert processing logic here */
      /*...*/
      if workstr = "" then leave
   end

BTW, using the PARSE instruction to do multiple variable assignments
is usually much faster than using individual assignment statements.
The following code excerpt from a date calculation routine I wrote
demonstrates this technique.

/*---------------------------------------------------------------*/
/*  Fill the Name-of-Month and Days-per-month arrays.            */
/*---------------------------------------------------------------*/
Parse value 'January February March April May June July' ,
   'August September October November December' with ,
   Month.1 Month.2 Month.3 Month.4 Month.5 Month.6 ,
   Month.7 Month.8 Month.9 Month.10 Month.11 Month.12
Parse value 31 28 31 30 31 30 31 31 30 31 30 31 with ,
   DaysInMonth.1 DaysInMonth.2 DaysInMonth.3 ,
   DaysInMonth.4 DaysInMonth.5 DaysInMonth.6 ,
   DaysInMonth.7 DaysInMonth.8 DaysInMonth.9 ,
   DaysInMonth.10 DaysInMonth.11 DaysInMonth.12

This piece of code runs 40% faster (CMS, TSO and PC) than the same
thing done using "Month.1 = 'January'" type assignments.  As always,
your mileage may vary depending on number of assignments, execution
platform and weather conditions.

Wayne

ug@linkoping.telesoft.se (Ulf G|ransson) (02/11/91)

In article <91039.085930GIAMPAL@auvm.auvm.edu> GIAMPAL@auvm.auvm.edu writes:
>that are contained in a Rexx string.  Suppose I do :
>
>                parse pull mystring
>
>Then I want to see individual characters in mystring.  I know I could
>use the index() function or substr(), but that seems wasteful, not to
>mention slow.

You could use the splitting capabilities of the PARSE instruction.
If you just want to see each character from left to right, the following
example could be used:

		tmp=mystring
		do i=1 until tmp=''
		  parse var tmp char +1 tmp
		  say 'Character #'i 'is "'char'"'
		  end

You could also pick out individual characters in the middle
of the string, using something like:

		i=7
		parse var mystring =i +1

but then I would rather use SUBSTR.

-- 
Ulf Goransson, Telesoft Europe AB         (ug@linkoping.telesoft.se)

And now for something completely different.

MADIF@ROHVM1.BITNET (Jim Foster) (02/12/91)

In article <Heg5w1w163w@wiffle.techbook.com>, waynet@wiffle.techbook.com (Wayne
Tilton) says:
>
>BTW, using the PARSE instruction to do multiple variable assignments
>is usually much faster than using individual assignment statements.
>The following code excerpt from a date calculation routine I wrote
>demonstrates this technique.
>
If you're going to compile your execs, I wouldn't do this.  The compiler is
able to assign values at compile time for simple assignment statements such
as:  Name = 'Jim'.  As far as I know, this is not true for parse statements
or in the above example if you forget the quotes.

Many people also find the simple assignment statement easier to maintain.
I've had to explain the use of a parse statement used to initialize variables
often enough that I don't bother with it anymore.