Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Replacement of a substring in a string

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message
hariibm

New User


Joined: 05 Feb 2007
Posts: 61
Location: Chennai

PostPosted: Wed Jul 23, 2008 2:09 pm    Post subject: Replacement of a substring in a string
Reply with quote

Hi,

I would like to replace a substring in a String with another substring.

Ex: 145 143 142 + 1422
suppose i want to replace 142 with 152, then the result string after replacemnet is :
145 143 152 + 1422

i.e it must change the substring only 142 by 152. but not 1422 to 152.
Please provide me a piece of code for the above requirement.
Please let me know if it is not clear.

Hari
Back to top
View user's profile Send private message

hariibm

New User


Joined: 05 Feb 2007
Posts: 61
Location: Chennai

PostPosted: Wed Jul 23, 2008 2:11 pm    Post subject: Reply to: Replacement of a substring in a string
Reply with quote

a small correction ..
1422 must not be changed to 1522. only the exact string ie. 142 must be changed to 152.
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10203
Location: italy

PostPosted: Wed Jul 23, 2008 2:27 pm    Post subject: Reply to: Replacement of a substring in a string
Reply with quote

give a proper definition of string/substring ...

as a general point , given the topic title, both strings should be substituted,
in your case the thing to be substituted should be named something like
"blank delimited thing"
but, what if there is no blank between the 142 and the +

the parsing algorithm would get a bit more complicated...

I assumed a completely unformatted thing, here is a bit of logic

position on the first char after the first delimiter
( or at the beginning of the string )

find the next delimiter
check the length of the field
if equal check the substring
carry on the substitution ( in place or ... )

position on the char after the found delimiter...
loop
Back to top
View user's profile Send private message
hariibm

New User


Joined: 05 Feb 2007
Posts: 61
Location: Chennai

PostPosted: Wed Jul 23, 2008 2:35 pm    Post subject:
Reply with quote

145 143 142 + 1422 is a string.
and 142 is the substring. the substring 142 is to be replaced by 152.
the next character after 142 must not be a digit. it can be space or + or - or /(totally 5 such chars). whichever of the above, the replacement should be done.
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Wed Jul 23, 2008 2:36 pm    Post subject:
Reply with quote

Code:


INSPECT your-string REPLACING ALL '142 ' BY '152 '



Kindly do some brainstorming ....
Back to top
View user's profile Send private message
hariibm

New User


Joined: 05 Feb 2007
Posts: 61
Location: Chennai

PostPosted: Wed Jul 23, 2008 2:39 pm    Post subject: Reply to: Replacement of a substring in a string
Reply with quote

Hi ashimer,
I can not use INSPECT because the lengths of old substring(to be replaced) and new substrings are not known. it can be replacement of 3 characters by 5 or 5 characters by 3 etc.
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Wed Jul 23, 2008 2:41 pm    Post subject:
Reply with quote

ok .. i didnt see your second post ... can you please post all the occurences and the expected o/p ?
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10203
Location: italy

PostPosted: Wed Jul 23, 2008 2:43 pm    Post subject: Reply to: Replacement of a substring in a string
Reply with quote

Quote:
the next character after 142 must not be a digit. it can be space or + or - or /(totally 5 such chars). whichever of the above, the replacement should be done.

bolded the dubious sentence..

if
145 143 9142 + 1422 were the string

given he way You expressed Your requirement ( only the termination )...
9142 would become 9152 right ??
Back to top
View user's profile Send private message
hariibm

New User


Joined: 05 Feb 2007
Posts: 61
Location: Chennai

PostPosted: Wed Jul 23, 2008 2:50 pm    Post subject:
Reply with quote

Hi enrico-sorichetti,
If the string is 145 143 9142 + 1422
and if I want to replace 142 by 152 in the above case, no replacement must be done since there is no 142 as 142 and 9142 represent different IDs.
The replacement algorithm must find the exact match and replace it with the new substring.
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10203
Location: italy

PostPosted: Wed Jul 23, 2008 2:58 pm    Post subject: Reply to: Replacement of a substring in a string
Reply with quote

since we are talking about string/substring substitution

I ask again what are the front/starting delimiters ?

You told about the termination delimiter
for parsing You must tell also the starting delimiter

they might be different ID's ( applcation terminology, not parsing )
still by the way You expressed Yor requirement ( termination only constraint )
my sample was correct given the wrong/lack_of information
are we dealing with full free form strings ??
Back to top
View user's profile Send private message
hariibm

New User


Joined: 05 Feb 2007
Posts: 61
Location: Chennai

PostPosted: Wed Jul 23, 2008 3:01 pm    Post subject:
Reply with quote

Hi ashimer,

Please find below various cases.
first line represents the actual string and the next line represents the string after replacement.

Assuming 142 is to be replaced by 152.
1. 145 143 142 + 1422
145 143 152 + 1422

2. 145 143 /142 + 1422
145 143 /152 + 1422

3. 145 143 -142 + 1422
145 143 -152 + 1422

4. 145 143 141 + 1422
145 143 141 + 1422 (since there is no match hence there is no replacement.)

Assuming 142 is to be replaced by 1423.

5. 145 143 142 + 1422
145 143 1423 + 1422


6. 145 143 142/ + 1422
145 143 1423/ + 1422

Assuming 1432 is to be replaced by 143.

7. 145 +1432 142 + 1422
145 +143 142 + 1422
Back to top
View user's profile Send private message
hariibm

New User


Joined: 05 Feb 2007
Posts: 61
Location: Chennai

PostPosted: Wed Jul 23, 2008 3:04 pm    Post subject:
Reply with quote

Hi enrico-sorichetti,
The possible delimiters are +, -, /, ;, # or space.
They appear both at the front and at the end.
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10203
Location: italy

PostPosted: Wed Jul 23, 2008 3:16 pm    Post subject: Reply to: Replacement of a substring in a string
Reply with quote

Parsing in cobol s not easy,
what is the source of Your data , would it be a viable solution to try to get it in a fixed format ??

anyway here is a pointer to an article about parsing in cobol ( uses reference modification )
http://www.simotime.com/cblrmp01.htm
very good to get an idea about parsing in cobol,
does not do exactly what You are asking for, but extrapolation should be easy
beware of the legal/copyright issues before using
Back to top
View user's profile Send private message
hariibm

New User


Joined: 05 Feb 2007
Posts: 61
Location: Chennai

PostPosted: Wed Jul 23, 2008 3:26 pm    Post subject:
Reply with quote

If I know the exact location where the replacement should be done, then let me know how to replace it with a new substring and form a new string.
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Wed Jul 23, 2008 6:25 pm    Post subject:
Reply with quote

hari,

here is an example of changing 142 to 152 ... work on it for all cases ..

Code:


01 ws-name.
   05 ws-name1 pic x(20) value '145 142 145+ 1422'.
01 ws-arr.
   05 ws-nam pic x(1) occurs 20 times.
01 ws-count    pic s9(4) comp value 1.
01 ws-fnd      pic x(1).
   88 ws-found value 'y'.
01 ws-numeric  pic x.
   88 ws-num   values '0' thru '9'.
01 ws-final       PIC X(20).
PROCEDURE DIVISION.
0001-MAIN.
    move spaces to ws-numeric
    move 'n' to ws-fnd
    move 1 to ws-count
    perform until ws-count >= 20
      move ws-name1(ws-count:1) to ws-nam(ws-count)
      compute ws-count = ws-count + 1
     end-perform
     move 1 to ws-count
     perform until ws-count = 19
       if ws-nam(ws-count) = '1' and
          ws-nam(ws-count + 1) = '4' and
          ws-nam(ws-count + 2) = '2'
       then
          move ws-nam(ws-count + 3) to ws-numeric
          if ws-num
          then move '142' to ws-final(ws-count : 3)
          else
          move '152' to ws-final(ws-count : 3)
          end-if
          compute ws-count = ws-count + 3
       else
          move ws-name1(ws-count : 1) to ws-final(ws-count : 1)
          compute ws-count = ws-count + 1
       end-if
     end-perform
     display 'final=' ws-final
     STOP RUN.



o/p

Code:


145 152 145+ 1422



The reference modification lengths will depend on the length of the char which you are trying to replace ...
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
This topic is locked: you cannot edit posts or make replies. Checking wether a string starts from ... sundaram.naveen CLIST & REXX 9 Thu Oct 06, 2016 11:56 am
No new posts Random Password (in string format) ge... ezhavendhan COBOL Programming 10 Mon Aug 29, 2016 3:18 pm
No new posts How to insert a lengthy string havin... vidyaa DB2 7 Thu Aug 25, 2016 5:20 pm
No new posts Which function does the Ampersand (&a... Andi1982 PL/I & Assembler 3 Wed Aug 24, 2016 2:05 pm
No new posts Need a help how to replace a string i... gurunath82 CLIST & REXX 12 Tue Jul 26, 2016 11:43 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us