View previous topic :: View next topic
|
Author |
Message |
ajaypmenon
New User
Joined: 30 Mar 2007 Posts: 21 Location: North Carolina
|
|
|
|
Hi,
I have a string say ' abcdefghijklmnopqrstuvwxyz'. This is assigned to the variable a1. i have to overwrite the substring 'def' by '123'. But when i try substr(a1,4,3) = '123' its not working.
So i had to code it as
a1 = substr(a1,1,3) || '123' || substr(a1,7,20)
It wuld be great if somebody can guide me to the proper way of doing it?
Ajay |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
Quote: |
But when i try substr(a1,4,3) = '123' its not working. |
If you would like someone to help, you need to post what happened. Telling us "its not working" says nothing. . .
Was some error thrown? Did you get an unexpected/undesired result? |
|
Back to top |
|
|
ajaypmenon
New User
Joined: 30 Mar 2007 Posts: 21 Location: North Carolina
|
|
|
|
Hi Dick,
Sorry about that. please find below the result i got when i ran it.
INVALID COMMAND NAME SYNTAX
3 *-* SUBSTR(A1,4,3) = '123'
+++ RC(-3) +++
***
A return code of -3 is being thrown.
Thanks,
Ajay |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
Code: |
oldst = " abcdefghijklmnopqrstuvwxyz"
where = pos("def",oldst)
if where > 0 then ,
newst = overlay("123",oldst,where)
else ,
newst = oldst
say ">>" || oldst || "<<"
say ">>" || newst || "<<"
|
|
|
Back to top |
|
|
superk
Global Moderator
Joined: 26 Apr 2004 Posts: 4652 Location: Raleigh, NC, USA
|
|
|
|
Also
Code: |
a1 = 'abcdefghijklmnopqrstuvwxyz'
Say a1
Parse Var a1 left 'def' right
If Length(left) > 0 | Length(right) > 0) Then a1 = left||'123'||right
Say a1
|
|
|
Back to top |
|
|
ajaypmenon
New User
Joined: 30 Mar 2007 Posts: 21 Location: North Carolina
|
|
|
|
Hi Enrico,
First Thanks a lot for the reply. I tried it out and its working. It solved my requirement. but just my curiosity one more quick question. Now its overwriting from the position which we are giving. If the new substring('123') is longer than the target string ('def'). it is overwriting the following characters.
e.g. : say overlay('12345','abcdefghijk',4)
will give 'abc12345ijk'
Can i make it as 'abc123456ghijk'. just like tso command
C ALL 'DEF' '123456'
Thanks a lot,
Ajay |
|
Back to top |
|
|
ajaypmenon
New User
Joined: 30 Mar 2007 Posts: 21 Location: North Carolina
|
|
|
|
Hi Kevin,
Thanks a lot for the reply. I tried it out and it will help the purpose. I am looking into parse possibilities. Just checking whether it can be handled if the substring to be changed is given in a variable. i will try it out .
Thanks,
Ajay |
|
Back to top |
|
|
Pedro
Global Moderator
Joined: 01 Sep 2006 Posts: 2547 Location: Silicon Valley
|
|
|
|
if your search string is a variable, you may need to use the INTERPRET instruction:
Code: |
INTERPRET "Parse Var a1 left '"|| def ||"' right"
|
|
|
Back to top |
|
|
ajaypmenon
New User
Joined: 30 Mar 2007 Posts: 21 Location: North Carolina
|
|
|
|
Hi Pedro,
Thanks a lot. I will try it now. |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
herre are two code snippets
one using overlay or substring concatenation depending on length checking,
the other one using always substring concatenation
Code: |
haystk = " abcdefghijklmnopqrstuvwxyz"
needle = "def"
ovrlay = "12345"
offset = pos(needle,haystk)
if offset = 0 then ,
newstk = haystk
else do
if length(needle) = length(ovrlay) then ,
newstk = overlay(ovrlay,haystk,offet)
else ,
newstk = substr(haystk,1,offset-1) || ovrlay || substr(haystk,offset+length(needle))
end
say ">>" || haystk || "<<"
say ">>" || newstk || "<<"
|
Code: |
haystk = " abcdefghijklmnopqrstuvwxyz"
needle = "def"
ovrlay = "12345"
offset = pos(needle,haystk)
if offset = 0 then ,
newstk = haystk
else ,
newstk = substr(haystk,1,offset-1) || ovrlay || substr(haystk,offset+length(needle))
say ">>" || haystk || "<<"
say ">>" || newstk || "<<"
exit
|
the results is the same, the first one is uselessly more complicated/sophisticated |
|
Back to top |
|
|
ajaypmenon
New User
Joined: 30 Mar 2007 Posts: 21 Location: North Carolina
|
|
|
|
Hi Enrico,
Thanks. |
|
Back to top |
|
|
Pedro
Global Moderator
Joined: 01 Sep 2006 Posts: 2547 Location: Silicon Valley
|
|
|
|
You may also need to include Enrico's code in a loop. Consider:
Code: |
haystk = "defabcdefghijklmdefnopqrstdefuvwxdefyz"
needle = "def"
|
|
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
nice catch, Pedro. |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
Here is the code for multiple occurrences of the needle
Code: |
haystk = " abcdefghijklmnopqrstuvwxyzdef"
haystk = " def_def_def_def_"
needle = "def"
ovrlay = "12345"
newstk = haystk
positn = pos(needle,newstk)
do while positn > 0
newstk = substr(newstk,1,positn-1) || ovrlay || substr(newstk,positn+length(needle))
positn = pos(needle,newstk,positn+length(ovrlay))
end
say ">>" || haystk || "<<"
say ">>" || newstk || "<<"
|
|
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
and here is a snippet to change only a particular occurrence of the needle
Code: |
haystk = " abcdefghijklmnopqrstuvwxyzdef"
haystk = " def_def_def_def_"
needle = "def"
ovrlay = "12345"
search = 2
newstk = haystk
positn = pos(needle,newstk)
haveit = 0
do while positn > 0
haveit = haveit + 1
if haveit = search then do
newstk = substr(newstk,1,positn-1) || ovrlay || substr(newstk,positn+length(needle))
leave
end
positn = pos(needle,newstk,positn+length(needle))
end
say ">>" || haystk || "<<"
say ">>" || newstk || "<<"
|
|
|
Back to top |
|
|
ajaypmenon
New User
Joined: 30 Mar 2007 Posts: 21 Location: North Carolina
|
|
|
|
Thank you all. |
|
Back to top |
|
|
|