My favorite feature of REXX and the one I miss most in other languages

I'm a pretty relaxed programmer.   I like a programming feature that is so intuitive that I can remember how to use it without a reference manual and without Google.

REXX was invented to make string processing easy, at a time when there were few choices.   That's an apology because you won't see things like regular expressions natively in REXX.  But here's the kicker - you don't need them.  REXX has a simple syntax that is easy to remember and use.  It is not as powerful as PERL or Python - it was not made for that audience. It is a tool that lets regular people be productive without remembering a ton of rules.
s = 'this is my string'
if I want the 3rd word, type :
say word(s, 3)
dt = '10/20/2030'
The feature I like most is the "parse var" command.
The basic form is:


parse var sourcevar target1 separator target2 separator2 ... .
The '.' character represents a throwaway item.  The last target in the command gets the remainder of the string. That's it (mostly).

So, if I want to convert this string (dt) into three variables representing day, month, and year:
parse var dt d '/' m '/' y
This creates 3 new variables: d, m, and y.

What if I have this string?

dt2 = '10-20-2013'
parse var dt d '-' m '-' y
Piece of cake.  Ok, what about a US phone number?


ph = '(123)456-7890
parse var ph '(' area ')' nxx '-' line
Again, easy peasy.

So, what happens if the user types something that doesn't match? Well, let's try:

ph2 = '(123)4567890

parse var ph2 '(' area ')' nxx '-' line

since there is no dash, all the values go into nxx and line is blank.

The net result is that you'll want to make sure you got the right information back -- as you would  with regular expressions.

Oh and you can use parse var to pull apart fixed format strings as well.  I didn't use that as often so I can't remember the syntax -- except that I remember it uses numbers.

Update: I looked it up. Again, easy peasy.  Just put a number before each field. That's the starting position, where one (1) is the first position in any string.. As always, if you want to ignore, use a '.' as variable name. use a plus sign ('+') to indicate a relative distance from the last number.  Last field gets the remainder.

parse var ph 2 area +3 . +1 nxx +3 . +1 line +4 .

You can read this as:
parse variable ph, start at position 2 using area take 3. Using the throw away, take 1. Using nxx take 3. Using throw away take 1. Using line take 4 and ignore the rest.

Oh and you can drop the dots if you don't want them. The rest remains the same.

With regular expressions, you have so many dialects  (Regex Buddy supports a whole bunch), you're likely to get them wrong (see Zawinski's observation on Regular Expressions).

To me, this REXX syntax is much simpler.  Now, of course, this kind of parsing doesn't actually have the full power of regular expressions.  But then again, it doesn't have the complexity either.

The fixed format parsing is the most complex form of this syntax and it isn't used that often anyway.

REXX is available on all IBM operating systems and for free on Linux, Windows, and derivatives. You can find implementations at the REXX language association website (http://www.rexxla.org ). It is an ANSI standard language with a description at http://www.rexxla.org/rexxlang/standards/j18pub.pdf


Comments

Popular Posts