## Min And Max Of Tokens In A StringA couple of days ago I had a need to take a REXX string comprising space-separated numbers and find their minimum and maximum values. Here's the technique I used. (When I say "space-separated" there can be one or more spaces between the numbers, but there has to be at least one.) The solution has three components: - The REXX SPACE function - to turn the list into a comma-separated string of numbers. (The second parameter is the number of so-called spaces to separate tokens with. The third is the actual character to use - in my case a comma.)
- The REXX MIN (or MAX) function to compute the minimum (or maximum) value from this comma-separated string. These functions take a set of parameters of arbitrary length and do the maths on them. Parameters are separated by commas, hence the need to use SPACE to make it so.
- INTERPRET to glue 1 and 2 together.
My need is relatively low volume, so the "health warning" about INTERPRET's performance is hardly relevant for Here's the code:
It's relatively straightforward, taking a list of numbers and returning the minimum and maximum. You'll notice it doesn't check that the tokens really And my code uses this to decide if I have a range of values or just a single one. In the former case it turns the pair of numbers into e.g. "1-5" and the latter just e.g. "4". Of course there are other ways to do minimum and maximum for a list of numbers but this one seems the simplest and most elegant to me. "6 months later me" might take a different view. |