View previous topic :: View next topic |
Author |
Message |
rasprasads Beginner
Joined: 10 Dec 2002 Posts: 59 Topics: 20 Location: Chennai
|
Posted: Wed Feb 04, 2004 12:04 am Post subject: Search and Replace |
|
|
I have a alphanumeric field WS-ADD X(192). I need to search for the sub-string 'XXXXX-9999' in this WS-ADD where X given here should be a numeric digit.
Then i need to replace the 'XXXXX-9999' to 'XXXXX- ' (XXXXX- followed by Spaces).
Can someone Please help me out ? _________________ Rasprasad S |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Wed Feb 04, 2004 5:37 am Post subject: |
|
|
Rasprasad,
You need to provide details like the language you want this logic. Assuming it is cobol ,isn't this a simple solution using INSPECT with replacing? Even though I haven't tested it out , something like this should work.
Code: |
WORKING-STORAGE SECTION.
01 WS-ADD PIC X(192).
01 WS-STRING PIC X(10).
01 WS-REPL PIC X(10).
PROCEDURE DIVISION.
MOVE ' RASPRASDS HAD TO REPL XXXXX-9999 12345-9999 '
TO WS-ADD
MOVE '12345-9999' TO WS-STRING
MOVE '12345- ' TO WS-REPL
INSPECT WS-ADD REPLACING ALL WS-STRING BY WS-REPL
DISPLAY 'WS-ADD AFER REPLACE:' WS-ADD
GOBACK.
|
In the above example I am replacing the 12345-9999 With '12345- ', but the other xxxxx-9999 remains as is.
Check this link for a detailed explanation of the INSPECT command with examples
http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/IGYL1101/3.20?DT=19930312093006
Hope this helps...
Cheers
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
rasprasads Beginner
Joined: 10 Dec 2002 Posts: 59 Topics: 20 Location: Chennai
|
Posted: Wed Feb 04, 2004 5:57 am Post subject: |
|
|
Kolusu, Thanks for your (as always) quick answer.
I know about INSPECT. But the problem here is that the XXXXX i have mentioned (12345 in your example) will not be fixed. It can be any numeric item. '00000' TO '99999'.
Can you give a solution for this case ?
Thanks,
Prasad _________________ Rasprasad S |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Wed Feb 04, 2004 6:05 am Post subject: |
|
|
rasprasads,
can' you just search for '-9999' and replace it with spaces?
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
Dibakar Advanced

Joined: 02 Dec 2002 Posts: 700 Topics: 63 Location: USA
|
Posted: Wed Feb 04, 2004 6:12 am Post subject: |
|
|
How about
88 FIVE-DIGIT-NUMBER VALUE 00000 thru 99999.
88 HIPHEN-AND-NINES VALUE '-99999'.
...
If (FIVE-DIGIT-NUMBER AND HIPHEN-AND-NINES) ... |
|
Back to top |
|
 |
rasprasads Beginner
Joined: 10 Dec 2002 Posts: 59 Topics: 20 Location: Chennai
|
Posted: Wed Feb 04, 2004 7:23 am Post subject: |
|
|
Kolusu, I need to check if the format of 'XXXXX-9999' exists. So i can not go for just searching '-9999'. Can UNSTRING or any function can be used ?
Dibakar, I do not understand your solution. This 5 digits + '-' + '9999' can be anywhere in a string of length 192. How can i use your solution then ?
Thanks _________________ Rasprasad S |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Wed Feb 04, 2004 8:43 am Post subject: |
|
|
Rasprasad,
I still don't get the exact requirement. Are you saying that you have data like this?
Code: |
RASPRASDS HAD TO REPL XXXXX-9999 12345-9999 ABCDE-9999
|
XXXXX-9999 <==== Here XXXXX is alphabetic
12345-9999 <==== here 12345 is numeric
ABCDE-9999 <==== Here XXXXX is alphabetic
Now you want to just replace the 12345-9999 to '12345- ' ?? or do you want to replace all like this
Code: |
XXXXX-9999 ====> 'XXXXX- '
12345-9999 <==== '12345- '
ABCDE-9999 <==== 'ABCDE- '
|
The latter case of replacing All is just simple. you can do it with one inspect statement.
Code: |
INSPECT WS-ADD REPLACING ALL '-9999' BY '- '
|
Can you show us an example of your input and desired output?
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
rasprasads Beginner
Joined: 10 Dec 2002 Posts: 59 Topics: 20 Location: Chennai
|
Posted: Wed Feb 04, 2004 10:20 am Post subject: |
|
|
Here is the input :
"12345-9999 HAS been changed to 23456-9999 AND ABCDE-9999'
Output :
"12345- HAS been changed to 23456- AND ABCDE-9999'
Is it clear now Kolusu ? _________________ Rasprasad S |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Wed Feb 04, 2004 11:14 am Post subject: |
|
|
Rasprasad,
The following code will give you the desired results.
Code: |
WORKING-STORAGE SECTION.
01 WS-ADD PIC X(192) VALUE
'12345-9999 HAS BEEN CHANGED TO 23456-9999 AND ABCDE-9999'.
01 WS-REPL-STRING PIC X(192) VALUE SPACES.
01 WS-POS1 PIC S9(04) COMP.
01 WS-POS2 PIC S9(04) COMP.
01 WS-NUM PIC X(05).
01 WS-SUB PIC S9(04) COMP.
PROCEDURE DIVISION.
PERFORM VARYING WS-SUB FROM 1 BY 1 UNTIL WS-SUB > 192
INITIALIZE WS-POS1 WS-POS2 WS-NUM
IF WS-ADD(WS-SUB : 1) = '-' AND WS-SUB > 5
COMPUTE WS-POS1 = WS-SUB - 5
MOVE WS-ADD(WS-POS1 : 5) TO WS-NUM
IF WS-NUM IS NUMERIC
COMPUTE WS-POS2 = WS-SUB + 1
MOVE WS-ADD (WS-SUB : 1)
TO WS-REPL-STRING (WS-SUB : 1)
MOVE SPACES TO WS-REPL-STRING (WS-POS2 : 4)
COMPUTE WS-SUB = WS-SUB + 4
ELSE
MOVE WS-ADD(WS-SUB : 1)
TO WS-REPL-STRING(WS-SUB : 1)
END-IF
ELSE
MOVE WS-ADD(WS-SUB : 1)
TO WS-REPL-STRING(WS-SUB : 1)
END-IF
END-PERFORM
DISPLAY WS-REPL-STRING.
|
Hope this helps...
Cheers
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
rasprasads Beginner
Joined: 10 Dec 2002 Posts: 59 Topics: 20 Location: Chennai
|
Posted: Thu Feb 05, 2004 2:03 am Post subject: |
|
|
Thanks Kolusu. I Changed the above logic to space out only '9999' from the last 4 digits and tested it. It works fine.
BTW Will there be any performace impact if the WS-ADD is of length say X(2000) (and this WS-ADD may be occcuring 20000 times in the input file) ?
Can we go for this solution in that case too ? What can be done in order to improve the performance ? _________________ Rasprasad S |
|
Back to top |
|
 |
Dibakar Advanced

Joined: 02 Dec 2002 Posts: 700 Topics: 63 Location: USA
|
Posted: Thu Feb 05, 2004 4:24 am Post subject: |
|
|
If you are wondering about performing the loop 20000 times then I guess there's no way out. You have a big record and your string can occur anywhere, so what else can be done. If you any tool or some function, it will also do the same thing. |
|
Back to top |
|
 |
rasprasads Beginner
Joined: 10 Dec 2002 Posts: 59 Topics: 20 Location: Chennai
|
Posted: Thu Feb 05, 2004 5:41 am Post subject: |
|
|
Yes Dibakar, i agree with you.
But my question for Kolusu is that if there is any way out to improve the performance since the PERFORM may be for 20000 times? _________________ Rasprasad S |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Thu Feb 05, 2004 6:40 am Post subject: |
|
|
Rasprasad,
The performance will take a slight hit when you are talking of scanning 2000 bytes for 20000 times. why don't you run a test and see if the run times are accetable. Just code this replace logic and run the program.
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
rasprasads Beginner
Joined: 10 Dec 2002 Posts: 59 Topics: 20 Location: Chennai
|
Posted: Thu Feb 05, 2004 6:52 am Post subject: |
|
|
Ok! Let me test this and soon update you on the slide in performance. Thanks anyway for giving a perfect solution !!! _________________ Rasprasad S |
|
Back to top |
|
 |
rasprasads Beginner
Joined: 10 Dec 2002 Posts: 59 Topics: 20 Location: Chennai
|
Posted: Thu Feb 05, 2004 8:23 am Post subject: |
|
|
I tested with a little data and it works thru fine without any hitch in performance as well as functionality.
When i get a huge data i will check the perfomrance again and do a comparison to see how the code affects the performance. _________________ Rasprasad S |
|
Back to top |
|
 |
|
|