IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Overwriting a part of string in Rexx.


IBM Mainframe Forums -> CLIST & REXX
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
ajaypmenon

New User


Joined: 30 Mar 2007
Posts: 21
Location: North Carolina

PostPosted: Thu Jun 26, 2008 1:10 am
Reply with quote

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
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Thu Jun 26, 2008 1:24 am
Reply with quote

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
View user's profile Send private message
ajaypmenon

New User


Joined: 30 Mar 2007
Posts: 21
Location: North Carolina

PostPosted: Thu Jun 26, 2008 1:29 am
Reply with quote

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
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10872
Location: italy

PostPosted: Thu Jun 26, 2008 1:30 am
Reply with quote

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
View user's profile Send private message
superk

Global Moderator


Joined: 26 Apr 2004
Posts: 4652
Location: Raleigh, NC, USA

PostPosted: Thu Jun 26, 2008 1:34 am
Reply with quote

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
View user's profile Send private message
ajaypmenon

New User


Joined: 30 Mar 2007
Posts: 21
Location: North Carolina

PostPosted: Thu Jun 26, 2008 1:47 am
Reply with quote

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
View user's profile Send private message
ajaypmenon

New User


Joined: 30 Mar 2007
Posts: 21
Location: North Carolina

PostPosted: Thu Jun 26, 2008 1:52 am
Reply with quote

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
View user's profile Send private message
Pedro

Global Moderator


Joined: 01 Sep 2006
Posts: 2545
Location: Silicon Valley

PostPosted: Thu Jun 26, 2008 2:56 am
Reply with quote

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
View user's profile Send private message
ajaypmenon

New User


Joined: 30 Mar 2007
Posts: 21
Location: North Carolina

PostPosted: Thu Jun 26, 2008 3:09 am
Reply with quote

Hi Pedro,
Thanks a lot. I will try it now.
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10872
Location: italy

PostPosted: Thu Jun 26, 2008 11:26 am
Reply with quote

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
View user's profile Send private message
ajaypmenon

New User


Joined: 30 Mar 2007
Posts: 21
Location: North Carolina

PostPosted: Thu Jun 26, 2008 6:16 pm
Reply with quote

Hi Enrico,
Thanks.
Back to top
View user's profile Send private message
Pedro

Global Moderator


Joined: 01 Sep 2006
Posts: 2545
Location: Silicon Valley

PostPosted: Thu Jun 26, 2008 9:52 pm
Reply with quote

You may also need to include Enrico's code in a loop. Consider:
Code:

haystk = "defabcdefghijklmdefnopqrstdefuvwxdefyz"
needle = "def"
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Fri Jun 27, 2008 5:09 am
Reply with quote

nice catch, Pedro.
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10872
Location: italy

PostPosted: Fri Jun 27, 2008 10:43 am
Reply with quote

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
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10872
Location: italy

PostPosted: Fri Jun 27, 2008 10:50 am
Reply with quote

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
View user's profile Send private message
ajaypmenon

New User


Joined: 30 Mar 2007
Posts: 21
Location: North Carolina

PostPosted: Mon Jun 30, 2008 8:11 pm
Reply with quote

Thank you all. icon_smile.gif
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> CLIST & REXX

 


Similar Topics
Topic Forum Replies
No new posts PARSE Syntax for not fix length word ... JCL & VSAM 7
No new posts Running REXX through JOB CLIST & REXX 13
No new posts Error to read log with rexx CLIST & REXX 11
No new posts isfline didnt work in rexx at z/OS ve... CLIST & REXX 7
No new posts run rexx code with jcl CLIST & REXX 15
Search our Forums:

Back to Top