IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Replace "&" with "AND"


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
soumen2255

New User


Joined: 25 Jun 2008
Posts: 24
Location: Pune

PostPosted: Wed Apr 10, 2013 4:15 am
Reply with quote

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
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2501
Location: Atlanta, Georgia, USA

PostPosted: Wed Apr 10, 2013 4:48 am
Reply with quote

Have you reviewed the INSPECT verb or use an In-Line PERFORM?
Back to top
View user's profile Send private message
mistah kurtz

Active User


Joined: 28 Jan 2012
Posts: 316
Location: Room: TREE(3). Hilbert's Hotel

PostPosted: Wed Apr 10, 2013 11:14 am
Reply with quote

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
View user's profile Send private message
soumen2255

New User


Joined: 25 Jun 2008
Posts: 24
Location: Pune

PostPosted: Thu Apr 11, 2013 3:38 am
Reply with quote

Thanks Bill and Mistah, That helped me alot , also helped me to understand more on the string manipulation logic.

Thanks again.
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Thu Apr 11, 2013 4:13 am
Reply with quote

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
View user's profile Send private message
mistah kurtz

Active User


Joined: 28 Jan 2012
Posts: 316
Location: Room: TREE(3). Hilbert's Hotel

PostPosted: Thu Apr 11, 2013 11:24 am
Reply with quote

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
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts Replace Multiple Field values to Othe... DFSORT/ICETOOL 12
No new posts PuTTY - "User is not a surrogate... IBM Tools 5
No new posts replace word 'MONTH' with current mon... SYNCSORT 11
No new posts Newbie Stuck on "Duplicate Datas... TSO/ISPF 5
Search our Forums:

Back to Top