MainframeSupports
tip week 50/2005:

The TRANSLATE function in REXX and PL/1 share the same characteristics; they work the same way and they have the same parameters in the same order. Such shared qualities are rarely seen when it comes to functions like TRANSLATE. Many of you may be familiar with TRANSLATE and this tip will only deal with a tiny detail for which TRANSLATE can be used very successful.

TRANSLATE understands three parameters. The first one is the character string you wish to translate from. The second parameter states what to translate to and the third parameter states what to translate from. An example:

SAY 'REXX --> 'TRANSLATE('wAvEs', 'WVS', 'wvs')
REXX --> WAVES
DISPLAY('PL/1 --> ' !! TRANSLATE('wAvEs', 'WVS', 'wvs'))
PL/1 --> WAVES

w, v and s from the first parameter are converted to W, V and S. All other characters remain unchanged. This is the preferred way of using TRANSLATE and the manuals are full of examples like the above. But what if you do like this:

SAY 'REXX --> 'TRANSLATE('gh/ef-abcd', '2002-05-11', 'abcd-ef-gh')
REXX --> 11/05-2002
DISPLAY('PL/1 --> ' !! TRANSLATE('gh/ef-abcd', '2002-05-11', 'abcd-ef-gh'))
PL/1 --> 11/05-2002

Hey, wait a minute..., what happened here. Well precisely the same as before, but the result was quite unexpected. Using the second parameter as input you are able to shift around the placements of characters. In the above example a date in standard DB2 format is translated to a quite different format.

When you have grasped the concept of this idea the possibilities seems to explode. What about shuffling around some bytes:

SAY 'REXX --> 'TRANSLATE('abcdefg', 'racecar', 'gfedcba')
REXX --> racecar
DISPLAY('PL/1 --> ' !! TRANSLATE('abcdefg', 'racecar', 'gfedcba'))
PL/1 --> racecar

Hey, something went wrong. No, thinking of it, it is OK. And you can also...

Previous tip in english        Sidste danske tip        Tip list