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
 

 

Overwriting a part of string in Rexx.

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> CLIST & REXX
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    Post subject: Overwriting a part of string in Rexx.
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

Site Director


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

PostPosted: Thu Jun 26, 2008 1:24 am    Post subject:
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    Post subject:
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

Global Moderator


Joined: 14 Mar 2007
Posts: 10203
Location: italy

PostPosted: Thu Jun 26, 2008 1:30 am    Post subject: Reply to: Overwriting a part of string in Rexx.
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

Moderator Team Head


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

PostPosted: Thu Jun 26, 2008 1:34 am    Post subject: Reply to: Overwriting a part of string in Rexx.
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    Post subject: Overwriting a part of the string.
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    Post subject:
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

Senior Member


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

PostPosted: Thu Jun 26, 2008 2:56 am    Post subject: Reply to: Overwriting a part of string in Rexx.
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    Post subject:
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

Global Moderator


Joined: 14 Mar 2007
Posts: 10203
Location: italy

PostPosted: Thu Jun 26, 2008 11:26 am    Post subject: Reply to: Overwriting a part of string in Rexx.
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    Post subject:
Reply with quote

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

Senior Member


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

PostPosted: Thu Jun 26, 2008 9:52 pm    Post subject: Reply to: Overwriting a part of string in Rexx.
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    Post subject:
Reply with quote

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

Global Moderator


Joined: 14 Mar 2007
Posts: 10203
Location: italy

PostPosted: Fri Jun 27, 2008 10:43 am    Post subject: Reply to: Overwriting a part of string in Rexx.
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

Global Moderator


Joined: 14 Mar 2007
Posts: 10203
Location: italy

PostPosted: Fri Jun 27, 2008 10:50 am    Post subject: Reply to: Overwriting a part of string in Rexx.
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    Post subject: Overwriting a part of string in Rexx.
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    IBMMAINFRAMES.com Support Forums -> CLIST & REXX All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts What is the command to check MODE of ... rohanthengal CLIST & REXX 6 Fri Nov 18, 2016 1:48 pm
This topic is locked: you cannot edit posts or make replies. Rexx to create GDG by scanning JJ result krish.deepu CLIST & REXX 5 Tue Oct 25, 2016 5:32 pm
No new posts Replace repeated JCL with looping REXX prino CLIST & REXX 3 Thu Oct 13, 2016 5:30 am
This topic is locked: you cannot edit posts or make replies. Need to code a Rexx PGM sundaram.naveen CLIST & REXX 18 Thu Oct 06, 2016 6:45 pm
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


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