View previous topic :: View next topic
|
Author |
Message |
soumen2255
New User
Joined: 25 Jun 2008 Posts: 24 Location: Pune
|
|
|
|
Hello,
I have to make a replacement of the character "&" with " and " in a cobol program. The result should be :
"You&Me" or "You & Me" should be replaced with "You and Me". Any advice |
|
Back to top |
|
|
Bill O'Boyle
CICS Moderator
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
|
|
|
|
Have you reviewed the INSPECT verb or use an In-Line PERFORM? |
|
Back to top |
|
|
mistah kurtz
Active User
Joined: 28 Jan 2012 Posts: 316 Location: Room: TREE(3). Hilbert's Hotel
|
|
|
|
As Bill has correctly pointed out, you can use INSPECT and PERFORM to achieve this.
Please refer to the below for a working code:
Code: |
WORKING-STORAGE SECTION.
01 WS-STRING PIC X(40) VALUE SPACES.
01 WS-TEMP-STRING PIC X(40) VALUE SPACES.
01 WS-REP-STR PIC X VALUE '&' .
01 WS-CNT PIC 9 VALUE ZERO.
01 WS-CHAR-CNT-B PIC 99 VALUE ZERO.
01 WS-CHAR-CNT-A PIC 99 VALUE ZERO.
01 WS-CHAR-CNT-P PIC 99 VALUE ZERO.
PROCEDURE DIVISION.
ACCEPT WS-STRING.
DISPLAY "VAR BEFORE: " WS-STRING.
INSPECT WS-STRING TALLYING WS-CNT FOR ALL WS-REP-STR
PERFORM UNTIL WS-CNT = ZERO
IF WS-CNT NOT = ZERO
INSPECT WS-STRING TALLYING WS-CHAR-CNT-B
FOR CHARACTERS BEFORE INITIAL WS-REP-STR
INSPECT WS-STRING TALLYING WS-CHAR-CNT-A
FOR CHARACTERS AFTER INITIAL WS-REP-STR
COMPUTE WS-CHAR-CNT-P = WS-CHAR-CNT-B + 2
EVALUATE TRUE
WHEN WS-STRING(WS-CHAR-CNT-B:1) = SPACE AND
WS-STRING(WS-CHAR-CNT-P:1) = SPACE
STRING WS-STRING(1:WS-CHAR-CNT-B) "AND"
WS-STRING(WS-CHAR-CNT-P:WS-CHAR-CNT-A)
DELIMITED BY SIZE INTO WS-TEMP-STRING
WHEN WS-STRING(WS-CHAR-CNT-B:1) = SPACE AND
WS-STRING(WS-CHAR-CNT-P:1) NOT = SPACE
STRING WS-STRING(1:WS-CHAR-CNT-B) "AND "
WS-STRING(WS-CHAR-CNT-P:WS-CHAR-CNT-A)
DELIMITED BY SIZE INTO WS-TEMP-STRING
WHEN WS-STRING(WS-CHAR-CNT-B:1) NOT = SPACE AND
WS-STRING(WS-CHAR-CNT-P:1) = SPACE
STRING WS-STRING(1:WS-CHAR-CNT-B) " AND"
WS-STRING(WS-CHAR-CNT-P:WS-CHAR-CNT-A)
DELIMITED BY SIZE INTO WS-TEMP-STRING
WHEN WS-STRING(WS-CHAR-CNT-B:1) NOT = SPACE AND
WS-STRING(WS-CHAR-CNT-P:1) NOT = SPACE
STRING WS-STRING(1:WS-CHAR-CNT-B) " AND "
WS-STRING(WS-CHAR-CNT-P:WS-CHAR-CNT-A)
DELIMITED BY SIZE INTO WS-TEMP-STRING
END-EVALUATE
MOVE WS-TEMP-STRING TO WS-STRING
END-IF
MOVE ZERO TO WS-CNT
MOVE ZERO TO WS-CHAR-CNT-A
MOVE ZERO TO WS-CHAR-CNT-B
MOVE ZERO TO WS-CHAR-CNT-P
MOVE SPACES TO WS-TEMP-STRING
INSPECT WS-STRING TALLYING WS-CNT FOR ALL WS-REP-STR
END-PERFORM.
DISPLAY "VAR AFTER: " WS-STRING.
STOP RUN. |
RUN JCL:
Code: |
//STEP01 EXEC PGM=PGMXX
//STEPLIB DD DSN=XXXX.YYY.LOAD,DISP=SHR
//SYSOUT DD SYSOUT=*
//SYSIN DD *
YOU& ME &I & US&THEM
//* |
Output:
Code: |
VAR BEFORE: YOU& ME &I & US&THEM
VAR AFTER: YOU AND ME AND I AND US AND THEM |
|
|
Back to top |
|
|
soumen2255
New User
Joined: 25 Jun 2008 Posts: 24 Location: Pune
|
|
|
|
Thanks Bill and Mistah, That helped me alot , also helped me to understand more on the string manipulation logic.
Thanks again. |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
It would be really good if you gave some more detailed examples of what is possible. If the data is entered by a user, and not validated for it, there are going to be examples of && and perhaps even &&&. You'll probably also have some &'s which shouldn't be there.
You show "mixed-case". If that reflects your data, then you should include that as a means of identifying some "suspect" data.
You can't just rip through a big bunch of data without preparation and thought.
Why do you need to change the & anyway?
What type of data is it?
Is it from now on going to be "trapped" at input?
Or are you going to get a new load of them on your data, or is your program going to aimlessly run on everything daily looking for them and potentially garbling something?
&ellow. Oh, look, I just made a typo. Now it's going to be andellow. |
|
Back to top |
|
|
mistah kurtz
Active User
Joined: 28 Jan 2012 Posts: 316 Location: Room: TREE(3). Hilbert's Hotel
|
|
|
|
Hi Soumen..You are welcome..but I have covered very specific cases..the one you have mentioned in your initial post..you can see in my run example. |
|
Back to top |
|
|
|