Search and Replace with the new DFSORT functional PTFs
MartinPacker 11000094DH Comments (2) Visits (12070)
Here's my first tip with the new UK90006 / UK90007 DFSORT PTFs. It's an apparently minor enhancement that I think people will like...
Suppose you want to search a file for a string in a particular position in each record and replace all matching instances with a different string. (I might do that to
In this example the field you are searching in is a 5-character string beginning in position 4. Hence
NOTE: I've already used two functions that are much more recent than the introduction of Symbols: IFTHEN and OVERLAY...
Now, that works pretty well - because of these two new functions introduced with PTFs UQ95213 / UQ95214 at the end of 2004. But suppose the format of the record changes. You would have to recode all such utterances for the new field positions. It would be much better to do it in one place. And that, in a nutshell, is the case for Symbols...
Continuing with our example, you might map the record with a SYMNAMES DD that points to the following definitions...
If you work through these definitions you'll realise that F3 starts at position 4 and is of length 5 bytes. So it matches our original example. Prior to UK90006 / UK90007 you could code:
OUTREC IFTHand the IFTHEN clause is
fully symbolic(ignoring the literal comparison string - which could itself be converted to a symbol.)
But suppose the data was reorganised so that this field moved. You'll notice that still requires you to change the OVERLAY clause to replace the
With UK90006 / UK90007 you can now code
and a reorganisation of the data just means changing the Symbols deck pointed to by the SYMNAMES DD. If you're sensible this will be in one permanent file and so it's just one edit (plus, of course, some testing).
What has changed is the ability to specify a position (for example for OVERLAY) using a symbol.
So, I think I've made the maintainability point. I think this enhancement also allows more elegant expression: You now refer to the field consistently by its symbolic name, rather than in two different ways in two different places.
As I said, it's a minor enhancement. But one I particularly like - as I mess around with DFSORT a lot and little annoys me more than clunkiness of expression and poor maintainability.
One final point: I mentioned above that you could replace the literal string values with symbols. Here's how to do it (in this example). Add two symbol definitions to the SYMNAMES DD:
and replace the statement with
and now you can change the search and replacement string values without changing all the occurrences of these strings in all your code - just in the one place.