View previous topic :: View next topic
|
Author |
Message |
hariibm
New User
Joined: 05 Feb 2007 Posts: 61 Location: Chennai
|
|
|
|
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 |
|
|
hariibm
New User
Joined: 05 Feb 2007 Posts: 61 Location: Chennai
|
|
|
|
a small correction ..
1422 must not be changed to 1522. only the exact string ie. 142 must be changed to 152. |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
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 |
|
|
hariibm
New User
Joined: 05 Feb 2007 Posts: 61 Location: Chennai
|
|
|
|
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 |
|
|
ashimer
Active Member
Joined: 13 Feb 2004 Posts: 551 Location: Bangalore
|
|
|
|
Code: |
INSPECT your-string REPLACING ALL '142 ' BY '152 '
|
Kindly do some brainstorming .... |
|
Back to top |
|
|
hariibm
New User
Joined: 05 Feb 2007 Posts: 61 Location: Chennai
|
|
|
|
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 |
|
|
ashimer
Active Member
Joined: 13 Feb 2004 Posts: 551 Location: Bangalore
|
|
|
|
ok .. i didnt see your second post ... can you please post all the occurences and the expected o/p ? |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
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 |
|
|
hariibm
New User
Joined: 05 Feb 2007 Posts: 61 Location: Chennai
|
|
|
|
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 |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
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 |
|
|
hariibm
New User
Joined: 05 Feb 2007 Posts: 61 Location: Chennai
|
|
|
|
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 |
|
|
hariibm
New User
Joined: 05 Feb 2007 Posts: 61 Location: Chennai
|
|
|
|
Hi enrico-sorichetti,
The possible delimiters are +, -, /, ;, # or space.
They appear both at the front and at the end. |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
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 |
|
|
hariibm
New User
Joined: 05 Feb 2007 Posts: 61 Location: Chennai
|
|
|
|
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 |
|
|
ashimer
Active Member
Joined: 13 Feb 2004 Posts: 551 Location: Bangalore
|
|
|
|
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
The reference modification lengths will depend on the length of the char which you are trying to replace ... |
|
Back to top |
|
|
|