Page 1 of 1

Remove spaces in a string

PostPosted: Mon Nov 24, 2014 5:15 pm
by venky720
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???

Re: Remove spaces in a string

PostPosted: Mon Nov 24, 2014 5:52 pm
by NicC
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?)

Re: Remove spaces in a string

PostPosted: Mon Nov 24, 2014 6:15 pm
by Aki88
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.

Re: Remove spaces in a string

PostPosted: Mon Nov 24, 2014 6:39 pm
by venky720
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

Re: Remove spaces in a string

PostPosted: Mon Nov 24, 2014 7:58 pm
by BillyBoyo
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.

Re: Remove spaces in a string

PostPosted: Mon Nov 24, 2014 8:01 pm
by Aki88
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.

Re: Remove spaces in a string

PostPosted: Mon Nov 24, 2014 8:03 pm
by Aki88
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.

Re: Remove spaces in a string

PostPosted: Mon Nov 24, 2014 9:55 pm
by venky720
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

Re: Remove spaces in a string

PostPosted: Mon Nov 24, 2014 10:28 pm
by venky720
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.