Replace special characters and find Numeric values in string



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

Replace special characters and find Numeric values in string

Postby MSURESH309 » Tue Apr 10, 2012 6:40 am

Hi friends,

I need a help on Cobol coding..

I would like to replace special characters with Spaces and then find any 9 digit numeric value( at end) is there in that string then perform some action. That numeric value should be 9 bytes length and is always last part of the string.If last 9 bytes are not 'numeric' then do nothing.Can you help me how we can do this?

Here is the example:-

#1.
I have a input string WS-VAR1 PIC X(30) like ABC-D.#E234123145

then special characters - . # should be replace by 'spaces' as below 'ABC D E234123145'

then search the string for last 9 characters..it will find 234123145..so Perform some action.

#2.

WS-VAR2 PIC X(30) value 'DEF&#F@G1234'

It's not having last 9 digits as 'numeric' so it does nothing.

Can you please share some ideas how I can do this?

Thanks,
Suresh.
MSURESH309
 
Posts: 17
Joined: Thu Apr 21, 2011 10:04 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Replace special characters and find Numeric values in string

 

Re: Replace special characters and find Numeric values in st

Postby BillyBoyo » Tue Apr 10, 2012 11:52 am

In your first example you change "special characters" to space before testing for numeric in last nine. In your second you don't do anything. Is this correct?

Can there be trailing spaces in the fields?

Can there be embedded spaces in the fields?

IBM Cobol doesn't really have "strings" in the same sense as you might find them in other languages, so to call them such introduces confusion. We'll never know exactly what you mean by "string" and you won't know what we mean.

You'll almost certainly use INSPECT. Possibly also FUNCTION REVERSE. Maybe UNSTRING. So look up all these, and STRING for good measure, in the manuals. Consider the questions and points above, Let us know the results of your considerations and anything you don't understand from the manuals,
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times

Re: Replace special characters and find Numeric values in st

Postby gokulNmf » Tue Apr 10, 2012 1:07 pm

Hi MSURESH309,
I have some doubts regarding the erquirements,
1) Is the length of the records always fixed? as you have mentioned is it always 30
2)Can there be trailing spaces in the fields? same as BillyBoyo's doubt

If record length is always 30 and last 9 digits that ie from 22-30 is what you want to check always then you can use Group variable.

If not then use inspect to find the end of record and read the last 9 bytes and then again use IS NUMERIC to find whethe its numeric or not.
Hope it clarifies ur doubt.
Cheers,
Gokul
User avatar
gokulNmf
 
Posts: 114
Joined: Sat Mar 28, 2009 6:41 pm
Location: India
Has thanked: 0 time
Been thanked: 0 time

Re: Replace special characters and find Numeric values in st

Postby MSURESH309 » Tue Apr 10, 2012 4:38 pm

Hi BillyBoyo ,

In the second example also I have to look for 9 digit numeric values..if it don't find i will do nothing. Actually it's always look for 9 digit fields at end(last non space).

This 30 character variable is a user entered variable, spaces may/may not be there.These are some examples for these values.

abc 12345
abcd
a#b#c-d 123-456-798
123-543-432

In these examples My program may find numeric(9) values only for last two examples.Then I will do some other performs for these two.

Gokul,

1. length is always fixed as x(30).
2. Yes trailing spaces will be there. last 9 digits are not fixed as 22-30.

Hope..I clarified your questions. Can you help me here.Thanks.
MSURESH309
 
Posts: 17
Joined: Thu Apr 21, 2011 10:04 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Replace special characters and find Numeric values in st

Postby BillyBoyo » Tue Apr 10, 2012 4:46 pm

Did you do the manual reading?

You can change the characters you want with INSPECT.

You can FUNCTION REVERSE your 30-byte field, so your trailing spaces become leading spaces.

Then UNSTRING delimited by space into a 9-byte field. Test the field for numeric. If it is, then you know you have a number. If you want to use that number, remember to FUNCTION REVERSE it again.

This may not be enough for your full requirement, but you'll let us know, I suspect....
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times

Re: Replace special characters and find Numeric values in st

Postby MSURESH309 » Wed Apr 11, 2012 7:04 am

Okay.

I am planning to do following steps.But still have some questions.

WS-VAR1 PIC X(30) value 'ABC-D.#E234/12/3145'

1. Inspect on WS-VAR1 to replace '#', '.', '/' by spaces

output would be --> ABC D E234 12 3145

Question:- Do I have to use INSPECT 3 times for 3 special characters.
1.INSPECT, for replacing '#' by SPACES.
2.INSPECT, for replacing '.' by SPACES.
3.INSPECT, for replacing '/' by SPACES.


2.Doing perform something like below..

perform varying source-index from 1 by 1
until source-index > source-size
if source-field(source-index:1) > space
add 1 to target-index
move source-field(source-index:1) to
target-field(target-index:1)
end-if
end-perform.

3.Now calculating the length of string and then will use Reference modification for numeric check like ws-new-var(len-of-str:9) is NUMERIC then do my work, else do nothing..

Can anyone throw some light on my approach?

Thanks.
MSURESH309
 
Posts: 17
Joined: Thu Apr 21, 2011 10:04 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Replace special characters and find Numeric values in st

Postby Robert Sample » Wed Apr 11, 2012 8:24 am

How do you plan to calculate the length of the "string" (since COBOL does not have strings)? If you use the length of WS-VAR1, you'll get back 30 every time, you know. Plus your post indicates you're going to use reference modification to isolate the last byte of the variable (len-of-str) and the next 8 bytes after that -- how's that going to work?

Did you read the COBOL Language Reference manual on the INSPECT statement to see what is allowed?
Robert Sample
Global moderator
 
Posts: 3367
Joined: Sat Dec 19, 2009 8:32 pm
Location: East Dubuque, Illinois
Has thanked: 1 time
Been thanked: 222 times

Re: Replace special characters and find Numeric values in st

Postby gokulNmf » Wed Apr 11, 2012 10:16 am

Hi Suresh,
1 step:
With tallying multiple charecters work, but i am not sure about the inspect with replacing will work or not. But nevertheless you can try giving all the replace with in a single statement and give a try.

2nd step:
My understanding: I think your trying to move the non space charecters to the target-field.
If so, instead of that you can use the reverse function, to reverse the string and after that you can find out the leading spaces using inspect with tallying by this way you can get the actuall length string.

move function reverse(source-field) to reverse-field
inspect reverse-field tallying ws-count for leading spaces
actuall-length=length(source-field) - ws-count

3rd Step:
Using the actuall length with reference modification you can find the last 9 bytes and check whether its numeric.
last-9bytes=reverse-field(ws-count+1:9)
if last-9bytes is numeric then
Your operation
end if

I hope this will be helpful.
Also please go through the manual for finding more on inspect verb as sugessted by Robert.
Cheers,
Gokul
User avatar
gokulNmf
 
Posts: 114
Joined: Sat Mar 28, 2009 6:41 pm
Location: India
Has thanked: 0 time
Been thanked: 0 time

Re: Replace special characters and find Numeric values in st

Postby MSURESH309 » Mon Apr 23, 2012 10:14 pm

Thanks Gokul..

I did code as below.this way worked for me to check if the last 9(excluding special characters) bytes are numeric or not..But would like to check if there is any 9 byte string(excluding special characters) exists anyware(fist nine, middle nine,end nine..etc, but it shoud have 9 bytes numbers + spl characters) in the data.My current code only works for last nine bytes. I am thinking for the options..Any suggestionscan be welcomed..Thanks..

[MOVE DATA-IN TO DATA-SRC.

INSPECT DATA-SRC REPLACING ALL '/' BY ' '.
INSPECT DATA-SRC REPLACING ALL '.' BY ' '.
INSPECT DATA-SRC REPLACING ALL '-' BY ' '.

MOVE SPACES TO DATA-TGT.
MOVE ZEROS TO CNT-TGT.
PERFORM VARYING CNT-SRC FROM 1 by 1
UNTIL CNT-SRC > 30
IF DATA-SRC(CNT-SRC:1) > SPACE
ADD 1 TO CNT-TGT
MOVE DATA-SRC(CNT-SRC:1) TO
DATA-TGT(CNT-TGT:1)
END-IF
END-PERFORM.



IF CNT-TGT > WS-9
COMPUTE DATA1-MINUS-EIGHT = CNT-TGT - 8
MOVE DATA-TGT(DATA1-MINUS-EIGHT:9) TO DATA-9
IF DATA-9 NUMERIC
------
------
ELSE
NEXT SENTENCE
END-IF
ELSE
NEXT SENTENCE
END-IF.
]
MSURESH309
 
Posts: 17
Joined: Thu Apr 21, 2011 10:04 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Replace special characters and find Numeric values in st

Postby BillyBoyo » Mon Apr 23, 2012 10:50 pm

You need to look at byte 1, for 9, byte 2 for 9, byte 3 for 9 up to byte 22 for 9.

What you do with multiples, is down to you. Whether to start from the back, the front, or somewhere in the middle, is down to you.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 264 times

Next

Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post