Replace special characters with spaces in a string

This is a Mainframe COBOL forum - you can post your queries on Mainframe COBOL, VS COBOL II, COBOL/370 , Enterprise COBOL

Moderators: dbzTHEdinosauer, Moderator Group

Post Reply
Salman
Member
Posts: 6
Joined: Sun Sep 08, 2013 2:20 pm

Replace special characters with spaces in a string

Post by Salman » Tue Feb 25, 2014 4:15 pm

Hi Folks,

Can anyone please let me know how can i replace the special characters in a string with spaces?
Ex: If i have a string 'ABC@123#d_efg)'
then it should give me 'ABC 123 d efg '

By using INSPECT we can do it provided if we know the special characters in advance but in my case i can't do it either because there are numerous special characters in my project and i can't use INSPECT for every special character and for every time.

so,I used the below logic to fix this but this seems not working.

77 ws-add pic x(50).
77 ws-var pic x(50).

move '1!2@cd#ef$gh%ij^klmn*&' to ws-add
*'1 2 cd ef gh ij klmn ' is my desired output
move ws-add to ws-var

PERFORM para-1 VARYING ws-index FROM 1 BY 1
UNTIL ws-index > 50

para-1:
if ws-var(ws-index:1) is numeric
move ws-var(ws-index:1) to ws-add(ws-index:1)

else if ws-var(ws-index:1) is alphabetic
move ws-var(ws-index:1) to ws-add(ws-index:1)
else
move spaces to ws-add(ws-index:1).

Can anyone throw some light on this ?

Thanks & Regards,
AAkheel.

William Collins
Active Member
Posts: 732
Joined: Thu May 24, 2012 4:07 am

Post by William Collins » Tue Feb 25, 2014 5:11 pm

You don't say what isn't working.

DikDude
Moderator
Posts: 1001
Joined: Fri Jul 22, 2011 8:39 am
Location: usa

Post by DikDude » Tue Feb 25, 2014 10:29 pm

Hello,
but in my case i can't do it either because there are numerous special characters in my project and i can't use INSPECT for every special character and for every time.
Why not?

As asked - what is Not working?
Have a good one

Salman
Member
Posts: 6
Joined: Sun Sep 08, 2013 2:20 pm

Post by Salman » Wed Feb 26, 2014 9:48 am

Hi Folks,

Sorry for incomplete post.
But as i mentioned, using Refernce Modification for every Feild,
like if it is of x(250),then it has to check for 250 times and it will make it very tedious and hence the performance of the program degrades.
So, i request to provide me an optimized way to fix this thing.

Thanks & Regards,
Aakheel.

William Collins
Active Member
Posts: 732
Joined: Thu May 24, 2012 4:07 am

Post by William Collins » Wed Feb 26, 2014 1:28 pm

What is the purpose of this removal of "special" characters?

If you can answer that, you may get some useful replies.

Perhaps you can explain what you mean by "tedious". It is a computer. It finds nothing tedious.

Salman
Member
Posts: 6
Joined: Sun Sep 08, 2013 2:20 pm

Post by Salman » Wed Feb 26, 2014 2:34 pm

Hi William,

Actually its an address feild to which it receives from another system so there might be some junk values like 'square'.*/.... for which i need to remove it and fill it with spaces(Only Numerics and Alphabetics allowed).
By performing the above code it satisfies but it will degrade the performance.
For example, if i have to generate a memo with x(1000), then it will check for all the 1000 letters present in the feild which i feel is not the better way and hence finding for an alternate way to optimize it.

Any help would be appretiable.

payilagamch
Member
Posts: 12
Joined: Tue Jan 14, 2014 1:20 pm
Contact:

Post by payilagamch » Wed Feb 26, 2014 2:48 pm

If you have a list of possible special characters you can use INSPECT with REPLACING.

INSPECT source-string REPLACING ALL '#' BY ' '
ALL '_' BY ' '
Payilagam Chennai
www.payilagam.com

Salman
Member
Posts: 6
Joined: Sun Sep 08, 2013 2:20 pm

Post by Salman » Wed Feb 26, 2014 3:34 pm

Hi Payilagamch,

As I mentioned above we can use Inspect only in the case if we know the special characters in advance but in my case there are large number of special charcters and sometimes may be junnk(square...).
Hence, it will not work here.

Salman
Member
Posts: 6
Joined: Sun Sep 08, 2013 2:20 pm

Post by Salman » Wed Feb 26, 2014 3:41 pm

Hi Payilagamch,

As I mentioned above we can use Inspect only in the case if we know the special characters in advance but in my case there are large number of special charcters and sometimes may be junnk(square...).
Hence, it will not work here.

William Collins
Active Member
Posts: 732
Joined: Thu May 24, 2012 4:07 am

Post by William Collins » Wed Feb 26, 2014 5:47 pm

This, then, is something to be done at the time the data is captured. You do it once, it is done.

If there is some absurd reason that it can't be done at data-capture, then how does the data get to your system? Is it stored on the other system then passed to you every time? Or is it passed to you once, so you have your "capture" and you do it at that point, once only.

If it is passed every time, you (your boss) should insist that the system which passes it does the validation. Then, when they realise what a pain that is, they will do it themselves at the point of capture.

Don't look at REPLACING, look at INSPECT .... CONVERTING, but you'd have to convert every unwanted value.

If you can't get good data given to you, then you have to look at making your code better. Although I don't like destorying the contents of a field, since you are doing it anyway, why have two copies of the field?

Also, since you only want to space-out the values you don't want, you doin't need to MOVE the bytes when you discover they are OK. Since you are looking at text, and fields will allow for maximum size, you should identify trailing space and stop your tests when you reach that. Do that with the same looping, but starting from the end of the field and going "backwards" until you get the first non-blank.

That'll speed your code up, but even more so if you don't have to do it because it is done at the correct point...

DikDude
Moderator
Posts: 1001
Joined: Fri Jul 22, 2011 8:39 am
Location: usa

Post by DikDude » Wed Feb 26, 2014 10:13 pm

One thing you might do is look for the valid characters rather than the invalid ones.

Then define an 88 level that includes all valid codes. A simple IF will do for each position (reference modification to move across the input data). When valid, leave alone otherwise move space.

What testing have yuou done to verify how much resource is needed.

It really doesn't matter as the data Must be correct.
Have a good one

NicC
Active Member
Posts: 650
Joined: Sun Jul 24, 2011 5:27 pm
Location: Down on the pig farm

Post by NicC » Thu Feb 27, 2014 3:13 am

Probably a reddefines of the field as a table would be more efficient than ref. mod. You could check each byte is NUMERIC or ALPHA nd set to BLANK if neither.
Regards
Nic

devendar
Member
Posts: 1
Joined: Wed Jun 14, 2017 4:14 pm
Location: Hyderabad

Post by devendar » Wed Jun 14, 2017 4:29 pm

Below condition will help to remove the unwanted values from field, if we receives the value other than the ALPHABETIC and NUMERIC below code will replaces the JUNK or unwanted values with "spaces".

01 STREET1-ADDRESS X(25)
01 ADDR1 REDEFINES STREET1-ADDRESS OCCURS 25 TIMES

PROCEDURE DIVISION.

OPEN INPUT INPUT-FILE
OUTPUT OUTPUT-FILE,

MOVE INPUT-ADDR1 TO STREET1-ADDRESS

IF (ADDR1 (SUB1) ALPHABETIC) OR
(ADDR1 (SUB1) NUMERIC)
NEXT SENTENCE
ELSE
MOVE SPACES TO ADDR1 (SUB1)
END-IF.
Thanks
Devendar

Post Reply

FREE TUTORIALS

Tutorials
Free tutorials from mainframegurukul
  • JCL Tutorial
    Covers all important JCL concepts.
  • Cobol Tutorial
    This tutorials covers all Cobol Topics from STRING to COMP-3.
  • DB2 Tutorial
    DB2 Tutorial focuses on DB2 COBOL Programming.
  • SORT Tutorial
    This Tutorial covers all important aspects of DFSORT with examples
  • CICS Tutorial
    This CICS tutorial covers CICS concepts and CICS Basics, CICS COBOL Programming.
Interview
Mainframe Interview questions



Other References
Mainframe Tools and others