On Tue, May 24, 2016 at 12:35 PM, Jon Paris <jon.paris@xxxxxxxxxxxxxx> wrote:
Looks like Steve’s subproc is the best starting point.
Very good starting point indeed! I think it puts you on the finish
line, or at most a few inches off it. I guess the thing that it
doesn't handle is having an actual space in your desired mask, such as
in your example of transforming 'ABCDEFGHI' into 'AB-CD-EFGH I'. So
instead of using a space as the "fill placeholder", you have to pick
something that can't show up in your mask.
Just because I tend toward the academic, I'll note that I thought of
something in other languages (and possibly available to RPG through
APIs or what have you) that would work pretty well for this: regular
expression substitution.
In Python, for example, the aforementioned 'ABCDEFGHI' -> 'AB-CD-EFGH
I' could be accomplished (interactively) like this:
import re
print re.sub(r'(..)(..)(....)(.)', r'\1-\2-\3 \4', 'ABCDEFGHI')
Or, a function that accepts an input string and returns the reformatted string:
import re
def edit(s):
return re.sub(r'(..)(..)(....)(.)', r'\1-\2-\3 \4', s)
It should be similar in any language that has what I'll call
"Perl-inspired" regular expressions. (This would include Java, Ruby,
PHP, JavaScript, and many others.) You could of course also pass the
match pattern and substitution pattern as parameters instead of making
them literals.
For this level of code conciseness, you'd have to convert your "edit
masks" to regex patterns. If that's not acceptable, and you have to
work directly with the edit masks, then I think you pretty much wind
up writing Steve's routine one way or another.
John Y.
As an Amazon Associate we earn from qualifying purchases.