Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!rpi!dali.cs.montana.edu!ogicse!pdxgate!parsely!percy!qiclab!techbook!waynet From: waynet@wiffle.techbook.com (Wayne Tilton) Newsgroups: comp.lang.rexx Subject: Re: looking at individual characters in a string Message-ID: Date: 9 Feb 91 22:02:16 GMT References: <91039.085930GIAMPAL@auvm.auvm.edu> Sender: bbs@techbook.com (waffle BBS) Organization: TECHbooks Bookstore, Beaverton OR Lines: 59 > 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