I don’t think we have enough information here. Will the numbers always be in the center of the string or can they be scattered? Will there be any spaces in the string to deal with?
I suspect you are going to have to do a byte by byte logic to get this done.
How would you want the following to come out?
“a1b2c3d4”
“1234abcd”
“a1b2cdef”
“ab cd ef”
If #NUM is a 1, there are no digits in the string.
If #NUM is a 3, there is a single digit in the string (no reversal required)
If #NUM is a 5, there is either a 2 digit string (there will be a sequence 101 in the lengths of #TEMPs, or two one digit strings (no reversal required)).
You can play with this depending on exactly what you require.
Hopefully, this does not “double post”; but the first post did not seem to work:
Here is an alternate program:
DEFINE DATA LOCAL
1 #IN (A30) INIT <‘1234abc5678hijklm5678’>
1 #OUT (A30)
1 #TEMP (A/1:30) DYNAMIC
1 #NUM (I2)
1 #LOOP (I2)
1 #TIMER (I2)
1 #ONE (A1)
END-DEFINE
*
SEPARATE #IN INTO #TEMP () WITH
RETAINED DELIMITERS ‘0123456789’
GIVING NUMBER #NUM
*
FOR #LOOP = 1 TO #NUM
MOVE #TEMP (#LOOP) TO #ONE
IF #ONE NE ’ ’ AND #ONE NE MASK (N)
MOVE #TEMP (#LOOP) (PM=I) TO #TEMP (#LOOP)
END-IF
END-FOR
*
COMPRESS #TEMP () INTO #OUT LEAVING NO SPACE
WRITE #IN / #OUT
END
Page 1 09-08-24 07:41:45
1234abc5678hijklm5678
1234cba5678mlkjih5678
On my PC the code above and Matthias’s code have about the same CPU time.
Would appreciate confirmation that the two programs are indeed what is required, then will do elapsed timings and also mainframe timings; then look to improve them.
On my PC, using the data string Matthias first used, the CPU time favored Matthias’s code by 53-55.
However, using a string of init <‘abcdabc56hijklmghijqwertasdfg’>, the numbers change dramatically; 64-26 in favor of my code.
Basically, Matthias’s code is more “consistent”, CPU time is mainly a function of the length of the string. My code is sensitive to the number of digits more than the length of the string.
So, you might want to consider what the expected data looks like when deciding which code excerpt to use.