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

How to perform STRIP Function in COBOL


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

New User


Joined: 23 Aug 2006
Posts: 93
Location: chennai

PostPosted: Fri Jul 18, 2008 10:08 pm
Reply with quote

I need to perform String function strip in COBOL,

Please suggest a simple method to perform this ..

Example:
Input
' INPUT '

Output Should be :
'INPUT'

Thanks
Ram
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Fri Jul 18, 2008 10:17 pm
Reply with quote

Hello,

You could code a loop and only move the bytes/values you want in the "output" string.
Back to top
View user's profile Send private message
ramfrom84

New User


Joined: 23 Aug 2006
Posts: 93
Location: chennai

PostPosted: Fri Jul 18, 2008 11:44 pm
Reply with quote

Thanks Dick for ur quick response,

But i would be difficult because I need to increment two counter , One for input field and other for Output field,

Is there any other possible solution ..
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Fri Jul 18, 2008 11:54 pm
Reply with quote

COBOL really doesn't lend itself to string manipulation like other languages. If you can't use a loop as Dick suggested, you're probably going to have to write a subroutine in another language to perform the function.
Back to top
View user's profile Send private message
Suresh Ponnusamy

Active User


Joined: 22 Feb 2008
Posts: 107
Location: New York

PostPosted: Fri Jul 18, 2008 11:58 pm
Reply with quote

Hi

If you want to remove only the leading SPACES then you can use many method like UNSTRING or INSPECT or using Reference Modification Move statement etc.

Please try the below one.

Declare 2 Working Storage variable

01 WS-SPACES
01 WS-INPUT

UNSTRING Input Delimited by SPACES INTO
WS-SPACES, WS-INPUT
END-UNSTRING.
Back to top
View user's profile Send private message
sonalsa

New User


Joined: 13 Sep 2007
Posts: 4
Location: charlotte

PostPosted: Sat Jul 19, 2008 1:32 am
Reply with quote

Hello ,

I have a similar scenario to be coded, where I need to remove special characters or spaces from a string.

Ex:
Say I have an input string as "~123Ab ^ erb &! erf *()_ ".
My desired output is "123Aberberf".

Could any one please help me how to get this coded in cobol.
Back to top
View user's profile Send private message
Suresh Ponnusamy

Active User


Joined: 22 Feb 2008
Posts: 107
Location: New York

PostPosted: Sat Jul 19, 2008 1:57 am
Reply with quote

Please refer the below given post.

ibmmainframes.com/viewtopic.php?t=32505

Here instead of Alphabet, you can define the list of Special Characters in the Working Storage Section and eliminate the same using Inspect Clause or through a Perform until loop.
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: Sat Jul 19, 2008 3:02 am
Reply with quote

This will remove all unwanted characters from a given STRING value and left-justify this value, padded with low-order spaces. The length of these example STRINGS are 256-Bytes.

Code:

CBL TRUNC(OPT)                                                           
           03  WS-XLATE-FROM-TBL   PIC  X(256).                           
           03  WS-XLATE-TO-TBL     PIC  X(256).                           
           03  WS-STRING           PIC  X(256).                           
           03  WS-WORK-STRING      PIC  X(256).                           
           03  WS-SUB              PIC  9(008)     BINARY.               
           03  WS-SUB-X            REDEFINES WS-SUB                       
                                   PIC  X(004).                           
      *                                                                   
           MOVE '~123Ab ^ erb &! erf *()_'                               
                                       TO WS-STRING.                     
           MOVE SPACES                 TO WS-XLATE-TO-TBL.               
           MOVE ZERO                   TO WS-SUB.                         
           MOVE 1                      TO TALLY.                         
      *                                                                   
      **** LOAD THE 'FROM-TBL' WITH ALL 256 POSSIBLE VALUES OF X'00'     
      **** THRU X'FF'.                                                   
      *                                                                   
           PERFORM UNTIL TALLY > LENGTH OF WS-XLATE-FROM-TBL             
               MOVE WS-SUB-X (4:)      TO WS-XLATE-FROM-TBL (TALLY:1)     
               ADD  1                  TO TALLY                           
               ADD  1                  TO WS-SUB                         
           END-PERFORM.                                                       
      *                                                                   
      **** NOW, TRANSFER THE BYTES YOU WOULD LIKE TO RETAIN FROM THE     
      **** 'FROM-TBL' TO THE 'TO-TBL'. NOTE THAT THE 'TO-TBL' HAS BEEN   
      **** INITIALIZED TO SPACES BEFOREHAND. IN THIS EXAMPLE, WE ARE     
      **** ONLY RETAINING LOWER-CASE LETTERS, UPPER-CASE LETTERS AND     
      **** NUMERICS 0-9.                                                 
      *                                                                   
           MOVE WS-XLATE-FROM-TBL (130:)                                 
                                       TO WS-XLATE-TO-TBL (130:9).       
           MOVE WS-XLATE-FROM-TBL (146:)                                 
                                       TO WS-XLATE-TO-TBL (146:9).       
           MOVE WS-XLATE-FROM-TBL (163:)                                 
                                       TO WS-XLATE-TO-TBL (163:8).       
           MOVE WS-XLATE-FROM-TBL (194:)                                 
                                       TO WS-XLATE-TO-TBL (194:9).       
           MOVE WS-XLATE-FROM-TBL (210:)                                 
                                       TO WS-XLATE-TO-TBL (210:9).       
           MOVE WS-XLATE-FROM-TBL (227:)                                 
                                       TO WS-XLATE-TO-TBL (227:8).       
           MOVE WS-XLATE-FROM-TBL (241:)                                 
                                       TO WS-XLATE-TO-TBL (241:10).       
      *                                                                   
      **** OK, WE'VE BUILT OUR TRANSLATE-TBLS, SO WE'LL KEEP THE LOWER   
      **** AND UPPER CASE LETTERS AS WELL AS NUMERICS 0-9 AND CONVERT     
      **** ALL OTHER BYTE-VALUES TO SPACE. AFTER THE 'INSPECT' THE       
      **** STRING WILL CONTAIN ' 123Ab   erb    erf     ', INTERSPERSED   
      **** WITH SPACES.                                                   
      *                                                                   
           INSPECT WS-STRING           CONVERTING WS-XLATE-FROM-TBL       
                                       TO WS-XLATE-TO-TBL.               
      *                                                                   
      **** MOVE THE CONVERTED STRING VALUE TO A WORK-STRING AND RESET     
      **** THE STRING TO SPACES. THEN, RE-POPULATE THE STRING AS A       
      **** LEFT-JUSTIFIED VALUE, IGNORING ALL SPACES.                     
      *                                                                   
           MOVE WS-STRING              TO WS-WORK-STRING.                 
           MOVE SPACES                 TO WS-STRING.                     
           MOVE ZERO                   TO WS-SUB.                         
           MOVE 1                      TO TALLY.                         
      *                                                                   
           PERFORM UNTIL TALLY > LENGTH OF WS-WORK-STRING                 
               IF  WS-WORK-STRING (TALLY:1) NOT = SPACE                   
                   ADD  1              TO WS-SUB                         
                   MOVE WS-WORK-STRING (TALLY:1)                         
                                       TO WS-STRING (WS-SUB:1)           
               END-IF                                                     
               ADD  1                  TO TALLY                           
           END-PERFORM.                                                   
      *                                                                   
   


Regards,

Bill
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Sat Jul 19, 2008 8:24 am
Reply with quote

Hello,

Quote:
But i would be difficult because I need to increment two counter , One for input field and other for Output field,
Then it is probably time to learn and get comfortable with such code. Once you have done just a bit, you will no longer find it difficult and intimidating icon_smile.gif
Back to top
View user's profile Send private message
sonalsa

New User


Joined: 13 Sep 2007
Posts: 4
Location: charlotte

PostPosted: Mon Jul 21, 2008 6:39 pm
Reply with quote

Thanks Bill...

Your soltion was a perfect fit for my situation ....thanks again for helping me ..
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 COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts COBOL ZOS Web Enablement Toolkit HTTP... COBOL Programming 0
No new posts Calling DFSORT from Cobol, using OUTF... DFSORT/ICETOOL 5
No new posts Generate random number from range of ... COBOL Programming 3
Search our Forums:

Back to Top