Remove spaces in a string



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Remove spaces in a string

Postby venky720 » Mon Nov 24, 2014 5:15 pm

I wnt to remove the spaces i've gathered in the variable

REC-INFO =  XXXX  ,AAAA ,SSSSSSS-DDDDD             ,00/0/0000,       -000000.00,          0.000000,          ,


Can this be done in COBOL in any way???
venky720
 
Posts: 4
Joined: Mon Nov 24, 2014 5:01 pm
Has thanked: 2 times
Been thanked: 0 time

Re: Remove spaces in a string

Postby NicC » Mon Nov 24, 2014 5:52 pm

As you posted it there were no spaces. Use the code tags to preserve your data, code and anything else that requires a fixed pitch font. Seek 9in the forum and you will find how to this. Clue: do not use the QuickReply button but the PostReply button. In fact, when you post you get the full editor anyway so you should investigate it's facilities before posting. (You take driving lessons before you drive a car, don't you?)
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic

These users thanked the author NicC for the post:
venky720 (Mon Nov 24, 2014 6:40 pm)
NicC
Global moderator
 
Posts: 3025
Joined: Sun Jul 04, 2010 12:13 am
Location: Pushing up the daisies (almost)
Has thanked: 4 times
Been thanked: 136 times

Re: Remove spaces in a string

Postby Aki88 » Mon Nov 24, 2014 6:15 pm

Hi Venky,

Yes, COBOL is very much capable of handling such conditions and more; there are various methods to achieve this, depending on your comfort level with COBOL.

Two quick methods are either by using 'INSPECT' or by using a well-coded 'PERFORM VARYING' construct with logic built to handle your requirement.

Try looking up the both of these in the COBOL programming manuals (link to manuals is at the top of page, or you can click here).
Try a small code piece using the same, if you get stuck, let us know and we might be able to help you.

Hth!

Regards.

These users thanked the author Aki88 for the post:
venky720 (Mon Nov 24, 2014 6:40 pm)
Aki88
 
Posts: 381
Joined: Tue Jan 28, 2014 1:52 pm
Has thanked: 33 times
Been thanked: 36 times

Re: Remove spaces in a string

Postby venky720 » Mon Nov 24, 2014 6:39 pm

Aki88 wrote:Hi Venky,

Yes, COBOL is very much capable of handling such conditions and more; there are various methods to achieve this, depending on your comfort level with COBOL.

Two quick methods are either by using 'INSPECT' or by using a well-coded 'PERFORM VARYING' construct with logic built to handle your requirement.

Try looking up the both of these in the COBOL programming manuals (link to manuals is at the top of page, or you can click here).
Try a small code piece using the same, if you get stuck, let us know and we might be able to help you.

Hth!

Regards.


Hi Aki,

Thanks.

Can you help me with an example code of using inspect.

Thanks & Regards,
Venky
venky720
 
Posts: 4
Joined: Mon Nov 24, 2014 5:01 pm
Has thanked: 2 times
Been thanked: 0 time

Re: Remove spaces in a string

Postby BillyBoyo » Mon Nov 24, 2014 7:58 pm

I'd like to see code with INSPECT removing spaces as well, please :-)

venky720, can you show how you constructed that field. The best way to get rid of the spaces is not to put them there in the first place.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: Remove spaces in a string

Postby Aki88 » Mon Nov 24, 2014 8:01 pm

Hello Venky,

Here is a fairly simple (and a li'l badly coded) PERFORM loop to get this requirement done:

016600     PERFORM VARYING X-COUNTER FROM 1 BY 1                   
016700               UNTIL X-COUNTER > LENGTH OF INPUT-VAR         
016800               IF  INPUT-VAR (X-COUNTER:1) = ' '             
016900                   CONTINUE                                   
017000               ELSE                                           
017100                   MOVE INPUT-VAR (X-COUNTER:1) TO           
017200                                   OUTPUT-VAR (X-OP-COUNTER:1)
017300                   ADD 1 TO X-OP-COUNTER                     
017400               END-IF                                         
017500     END-PERFORM.                                             
017600     DISPLAY 'OUTPUT-VAR:' OUTPUT-VAR.                       


The variables are as below:

009510 01  VARIABLE-DEFINITIONS.                                       
009600     03  INPUT-VAR                        PIC X(101) VALUE       
009700     '  XXXX  ,AAAA ,SSSSSSS-DDDDD             ,00/0/0000,       -
009800-    '000000.00,          0.000000,          ,'.                 
009900     03  OUTPUT-VAR                       PIC X(101).             
010000     03  COUNTER-VARIABLES.                                       
010010         05  X-COUNTER                    PIC 9(3) VALUE ZEROS.   
010100         05  X-OP-COUNTER                 PIC 9(3) VALUE 1.       


The output is as below:

OUTPUT-VAR:XXXX,AAAA,SSSSSSS-DDDDD,00/0/0000,-000000.00,0.000000,, 


Regards.
Aki88
 
Posts: 381
Joined: Tue Jan 28, 2014 1:52 pm
Has thanked: 33 times
Been thanked: 36 times

Re: Remove spaces in a string

Postby Aki88 » Mon Nov 24, 2014 8:03 pm

Hello Billy,

:oops: Didn't see your post; we rarely use INSPECTs/STRING/UNSTRINGs (always prefer a PERFORM loop), but yeah, let me try that as well beside PERFORM VARYINGs I mean. :D

Cheers.
Aki88
 
Posts: 381
Joined: Tue Jan 28, 2014 1:52 pm
Has thanked: 33 times
Been thanked: 36 times

Re: Remove spaces in a string

Postby venky720 » Mon Nov 24, 2014 9:55 pm

Aki88 wrote:Hello Venky,

Here is a fairly simple (and a li'l badly coded) PERFORM loop to get this requirement done:

016600     PERFORM VARYING X-COUNTER FROM 1 BY 1                   
016700               UNTIL X-COUNTER > LENGTH OF INPUT-VAR         
016800               IF  INPUT-VAR (X-COUNTER:1) = ' '             
016900                   CONTINUE                                   
017000               ELSE                                           
017100                   MOVE INPUT-VAR (X-COUNTER:1) TO           
017200                                   OUTPUT-VAR (X-OP-COUNTER:1)
017300                   ADD 1 TO X-OP-COUNTER                     
017400               END-IF                                         
017500     END-PERFORM.                                             
017600     DISPLAY 'OUTPUT-VAR:' OUTPUT-VAR.                       


The variables are as below:

009510 01  VARIABLE-DEFINITIONS.                                       
009600     03  INPUT-VAR                        PIC X(101) VALUE       
009700     '  XXXX  ,AAAA ,SSSSSSS-DDDDD             ,00/0/0000,       -
009800-    '000000.00,          0.000000,          ,'.                 
009900     03  OUTPUT-VAR                       PIC X(101).             
010000     03  COUNTER-VARIABLES.                                       
010010         05  X-COUNTER                    PIC 9(3) VALUE ZEROS.   
010100         05  X-OP-COUNTER                 PIC 9(3) VALUE 1.       


The output is as below:

OUTPUT-VAR:XXXX,AAAA,SSSSSSS-DDDDD,00/0/0000,-000000.00,0.000000,, 


Regards.


Hi Aki,

Thanks, I'll try the above code and let you know how it works.

Thanks & Regards,
Venky
venky720
 
Posts: 4
Joined: Mon Nov 24, 2014 5:01 pm
Has thanked: 2 times
Been thanked: 0 time

Re: Remove spaces in a string

Postby venky720 » Mon Nov 24, 2014 10:28 pm

venky720 wrote:
BillyBoyo wrote:I'd like to see code with INSPECT removing spaces as well, please :-)

venky720, can you show how you constructed that field. The best way to get rid of the spaces is not to put them there in the first place.



Billy, the code goes something like this

01  XYZ-SEQ-REC-INFO.

           05  XYZ-REC-DETAILS.
               10  XYZ-ASD                  PIC X(05).
               10  FILLER                   PIC X(04).
               10  FILLER                   PIC X(01) VALUE ','.
               10  XYZ-QWE                  PIC X(06).
               10  FILLER                   PIC X(01) VALUE ','.
               10  XYZ-RTY                  PIC X(30).
               10  FILLER                   PIC X(01) VALUE ','.
               10  XYZ-DT                   PIC X(10).
               10  FILLER                   PIC X(01) VALUE ','.
               10  XYZ-AMT                  PIC X(15).
               10  FILLER                   PIC X(01) VALUE ','.
               10  XYZ-UNITS                PIC X(18).
               10  FILLER                   PIC X(01) VALUE ','.
               10  XYZ-RMRK                 PIC X(10).
               10  FILLER                   PIC X(01) VALUE ','.
           05  FILLERS. 


And then i move values to it as the length of values are not always constant i get spaces in between.

Sorry for the repost.
venky720
 
Posts: 4
Joined: Mon Nov 24, 2014 5:01 pm
Has thanked: 2 times
Been thanked: 0 time


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post