Replacing a string with another string

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
aravindunni31
Participant
Posts: 27
Joined: Mon Jul 09, 2012 6:51 am
Location: Chennai

Replacing a string with another string

Post by aravindunni31 »

I have strings
MumbaiBangaloreChennai,
NaviMumbaiBangaloreChennai,
MumbaiPuneBangaloreKolkataChennai,
PuneBangaloreKolkataChennai,

I need to replace Kolkata with Calcutta if it exists in the string. How it can be achieved? This is in Datastage 8.1
Regards,
Aravind V A
prasson_ibm
Premium Member
Premium Member
Posts: 536
Joined: Thu Oct 11, 2007 1:48 am
Location: Bangalore

Re: Replacing a string with another string

Post by prasson_ibm »

Hi,

I think using Index function,u can achieve your requirment.Try serching in this forum,you will get multiple threads.

Thanks
Prasoon
aravindunni31
Participant
Posts: 27
Joined: Mon Jul 09, 2012 6:51 am
Location: Chennai

Post by aravindunni31 »

Thanks Prasoon. This will solve my query.
Regards,
Aravind V A
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

No, it will help you find the string but does nothing to replace it. How are you planning on doing the actual replacement?
-craig

"You can never have too many knives" -- Logan Nine Fingers
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Use the pxEreplace() function (which you can find on DSXchange) or upgrade to version 9.1 and use Ereplace() or Change() function.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
bhasds
Participant
Posts: 79
Joined: Thu May 27, 2010 1:49 am

Post by bhasds »

Hi aravindunni31,
The below logic may be help full-

Code: Select all

If Index(InputCol,"Kolkata",1) =0 Then InputCol Else InputCol [1,Index(InputCol,"Kolkata",1)-1] :"Calcutta":InputCol [Index(InputCol,"Kolkata",1) +7, Index(InputCol, Right(InputCol,1),1)]
Please correct me if I am wrong.
aravindunni31
Participant
Posts: 27
Joined: Mon Jul 09, 2012 6:51 am
Location: Chennai

Post by aravindunni31 »

Thanjs . Will try it
Regards,
Aravind V A
aravindunni31
Participant
Posts: 27
Joined: Mon Jul 09, 2012 6:51 am
Location: Chennai

Post by aravindunni31 »

ray.wurlod wrote:Use the pxEreplace() function (which you can find on DSXchange) or upgrade to version 9.1 and use Ereplace() or Change() function. ...
I am doing this in a parallel job using a transformer. EReplace function is not there. Can you please tell me what are the functions used to achieve this
Regards,
Aravind V A
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

ray.wurlod wrote:Use the pxEreplace() function (which you can find on DSXchange)
-craig

"You can never have too many knives" -- Logan Nine Fingers
PhilHibbs
Premium Member
Premium Member
Posts: 1044
Joined: Wed Sep 29, 2004 3:30 am
Location: Nottingham, UK
Contact:

Post by PhilHibbs »

Sadly I must advise that you DO NOT use the pxEreplace function. This is from IBM support:
IBM wrote:Good Morning,

I did some further research and found some more information:

It would seem that there is no way to free the memory that is allocated in a C transform routine. You should not use malloc in a transform routine.

We suggest that if you have a complex function then you may want to consider using a BuildOp instead. Calling C Transform routines is really meant for routines that return native C data types (primitive data types). Complex functions should be created as BuildOp's or Custom C stages.

I hope this advice helps

Many thanks
Mark H
This confirms my suspicion that the char* return type is almost entirely useless. The only safe thing that you can return with a char* routine is a fixed constant string such as an error message. It baffles me why IBM do not allow std::string as a return type, as this is precisely the reason that std::string was invented.

So, how do BuildOps work? I've never done one.

Getting back to the OP's problem - for simple cases, where you are sure that you have only one occurrence of the search string, then the index function can be used to find the string and you can slice it yourself in a derivation. Something like this, adjust slightly if your string might be longer than 999 characters:

Code: Select all

=If index(input,"Kolkata") = 0 Then input Else input[1,index(input,"Kolkata")-1] : "Calcutta" : input[index(input,"Kolkata")+7,999])
The index(input,"Kolkata") call should probably be moved out into a stage variable for efficiency.
Phil Hibbs | Capgemini
Technical Consultant
saidvs
Participant
Posts: 12
Joined: Thu Jul 28, 2005 10:41 am

Re: Replacing a string with another string

Post by saidvs »

try this

Convert('KolkataMumai','Calcutta','Kolkata') it works in parallel transformer too..
aravindunni31
Participant
Posts: 27
Joined: Mon Jul 09, 2012 6:51 am
Location: Chennai

Re: Replacing a string with another string

Post by aravindunni31 »

Convert Wont work for this scenario. Convert will convert only for one character. Not string
Regards,
Aravind V A
PhilHibbs
Premium Member
Premium Member
Posts: 1044
Joined: Wed Sep 29, 2004 3:30 am
Location: Nottingham, UK
Contact:

Re: Replacing a string with another string

Post by PhilHibbs »

saidvs wrote:try this
Convert('KolkataMumai','Calcutta','Kolkata') it works in parallel transformer too..
That will convert the third parameter replacing 'K' -> 'C', 'o's -> 'a', 'k' -> 'c', 'a' -> 'u', etc., and removing all 'M', 'u', 'm' and 'i' characters. Not exactly what the OP wanted.
Phil Hibbs | Capgemini
Technical Consultant
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

If you're not going to use pxEreplace() then you need a combination of Index() function and substring operators. For example:

Code: Select all

sv1  <--  Index(InLink.TheString, "Kolkata", 1)
sv2  <--  If sv1 Then InLink.TheString[1,sv1-1] : "Calcutta" : InLink.TheString[sv1+7,Len(InLink.TheString)-7]
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Post Reply