View previous topic :: View next topic
|
Author |
Message |
ashutosh.pr
New User
Joined: 13 Apr 2007 Posts: 36 Location: Pune
|
|
|
|
Hi,
We have to implement a logic where we have to replace a part of a string which has value ABC to a value DEF.
The structure of the string is such that the value ABC can appear in the last three bytes or three bytes before the last byte.
Illustrating:
The value can be :
FFFPABC1
or
FFFP1ABC
We have to replace the ABC with DEF so that we get the corresponding values as below:
FFFPDEF1
or
FFFP1DEF
Following are the variables declarations:
Code: |
01 WS-VARS.
10 WS-VARS-APPLICATION PIC X(03) VALUE SPACES.
10 WS-VARS-VAL PIC X(01) VALUE SPACES.
10 WS-VARS-FEED.
15 WS-VARS-TYPE1 PIC X(03) VALUE SPACES.
88 WS-VARS-ABC1 VALUE 'ABC'.
15 WS-VARS-FILL1 PIC X(01) VALUE SPACES.
10 WS-VARS-FEED2 REDEFINES WS-VARS-FEED.
15 WS-VARS-FILL2 PIC X(01).
15 WS-VARS-TYPE2 PIC X(03).
88 WS-VARS-ABC2 VALUE 'ABC'.
|
Right now we are using the following logic:
Code: |
MOVE WS-GROUP-VAR TO WS-VARS
IF WS-VARS-ABC1
MOVE 'DEF' TO WS-VARS-TYPE1
ELSE
IF WS-VARS-ABC2
MOVE 'DEF' TO WS-VARS-TYPE2
END-IF
END-IF
MOVE WS-VARS TO WS-GROUP-VAR |
Where WS-GROUP-VAR contains the value FFFPABC1 or FFFP1ABC
I just wanted to know that if instead of the above logic, we use the below logic, is it going to be costly (in terms of CPU usage), as we have been told that INSPECT function is costly as compared to the above logic of using IF?
Code: |
INSPECT WS-GROUP-VAR REPLACING ALL 'ABC' BY 'DEF' |
We are sure that there would not be any ABC anywhere else in the string except the last 4 bytes.
Thanks,
Ashutosh |
|
Back to top |
|
|
Bill O'Boyle
CICS Moderator
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
|
|
|
|
Instead of using "REPLACING", substitute with "CONVERTING", change "BY" to "TO" and remove "ALL".
INSPECT CONVERTING (as illustrated, using literals) generates (under the covers) an in-line Assembler TR, whereas, REPLACING always causes a BALR (CALL) to a COBOL run-time routine.
Code: |
INSPECT WS-GROUP-VAR CONVERTING 'ABC' TO 'DEF'
|
Bill |
|
Back to top |
|
|
Terry Heinze
JCL Moderator
Joined: 14 Jul 2008 Posts: 1249 Location: Richfield, MN, USA
|
|
|
|
Bill, in this case, shouldn't the REPLACING option be used instead of the CONVERTING? I don't have mainframe access at present, but wouldn't 'AXC' be converted to 'DBF' which is not the desired result. |
|
Back to top |
|
|
Bill O'Boyle
CICS Moderator
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
|
|
|
|
Terry,
The INSPECT REPLACING example (which the OP supplied), was coded as "Replace all 'ABC' with 'DEF'".
I just got rid of REPLACING, BY and ALL and substituted with CONVERTING and TO. The ALL was removed altogether as it wasn't necessary.
To tell you the truth, if that's not what the OP wanted, then I missed it by a mile.
Bill |
|
Back to top |
|
|
Terry Heinze
JCL Moderator
Joined: 14 Jul 2008 Posts: 1249 Location: Richfield, MN, USA
|
|
|
|
I also prefer the CONVERTING to REPLACING for the reasons you've explained, but my interpretation of how CONVERTING works is that all As will be converted to Ds, all Bs to Es, and all Cs to Fs given the following statement:
Code: |
INSPECT WS-GROUP-VAR CONVERTING 'ABC' TO 'DEF' |
As I mentioned, without mainframe access, I can't test this. |
|
Back to top |
|
|
Terry Heinze
JCL Moderator
Joined: 14 Jul 2008 Posts: 1249 Location: Richfield, MN, USA
|
|
|
|
Ashutosh,
I also have heard that INSPECT is more costly than IF, but it depends on how many times this conversion is done. CPU speeds being what they are, it would take a lot of these to notice much difference I would suspect. If you're really that concerned, your IF statements should suffice as long as the fields always remain defined as they are, and you can guarantee that the ABC, if it is present, will always be in the last 4 bytes of the 8 byte field. As Bill mentioned, the ALL is not needed given your conditions. |
|
Back to top |
|
|
ashutosh.pr
New User
Joined: 13 Apr 2007 Posts: 36 Location: Pune
|
|
|
|
Thank you very much Terry!
----------
Ashutosh |
|
Back to top |
|
|
Bill O'Boyle
CICS Moderator
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
|
|
|
|
The INSPECT example, regardless whether it is using REPLACING or CONVERTING, will not work as expected.
What it will do is to change all occurences of 'A' to 'D', 'B' to 'E' and 'C' to 'F'.
So, if you know where the the target literal can be found in the field, then check for it there. Otherwise, an in-line PERFORM using reference modification will work as well.
Note that if you use an in-line PERFORM, the UNTIL value will be the target field length minus two.
For example, if the field is 20-bytes long, the UNTIL value will be 18, because the last valid byte-position to check for the three-byte literal will be 18.
Bill |
|
Back to top |
|
|
|