Routines to do EReplace Function

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
sarathchandrakt
Participant
Posts: 50
Joined: Fri Aug 29, 2014 1:32 pm
Location: Mumbai

Routines to do EReplace Function

Post by sarathchandrakt »

Hi,

I have a requirement to convert all alphabets in a field to convert to symbols. Right now, I am using EReplace to do this in a transformer. But this requirement is getting extended to multiple fields. I was wondering if I can write this code in a Routine and call it from each field so that we don't have to update each field when any requirement changes.

Current Code Example:
Ereplace(Ereplace(Ereplace(Ereplace(Lnk_In.Data,'A','!'),'B','@'),'C','#'),'D','$')

I have never written a routine before nor used a routine. Any help is appreciated.

Thanks.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

How are your C++ skills? That's what parallel routines are written in.
-craig

"You can never have too many knives" -- Logan Nine Fingers
sarathchandrakt
Participant
Posts: 50
Joined: Fri Aug 29, 2014 1:32 pm
Location: Mumbai

Post by sarathchandrakt »

Unfortunately, after a second look, Routines might not be the option we are looking for at this point in time. From what I understand, if we take Parallel Routine path, we should make some changes in administrator to compile C++ code which would require a lot of approvals. And we also don't have a plan to migrate this C++ compiled files to different environments. Server Routines are not an option as we have too many parallel jobs. So, we might have to stick with EReplace on each field.

If anyone else have any ideas apart from routines, please feel free to mention.
qt_ky
Premium Member
Premium Member
Posts: 2895
Joined: Wed Aug 03, 2011 6:16 am
Location: USA

Post by qt_ky »

Try using a single Convert() function call to replace all of the EReplace() function calls.

https://www.ibm.com/support/knowledgece ... tions.html

Also in the Transformer stage you can do a multiple find and replace of the derivations on selected columns. If you're doing the same thing 50 places you can change them all at once.

Craig, get some coffee... :lol:
Choose a job you love, and you will never have to work a day in your life. - Confucius
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Yeah, stopped reading at "write this code in a Routine" and replied... then got my coffee. :(
-craig

"You can never have too many knives" -- Logan Nine Fingers
UCDI
Premium Member
Premium Member
Posts: 383
Joined: Mon Mar 21, 2016 2:00 pm

Post by UCDI »

Ive forgotten the details of e-replace, but it may matter in your real code that the result of the inner e-replaces is replaced by the outer calls if it matches. my code here does NOT do that; this code is for a simple this letter to that letter without any internal overlap. If that matters, I can show you how to handle it, its just more work. do you need it?

anyway, if you comment out main and compile this as a compatible datastage object, this should get you close.

//Ereplace(Ereplace(Ereplace(Ereplace(Lnk_In.Data,'A','!'),'B','@'),'C','#'),'D','$')

#include<string>
#include<iostream> //for main /debugging.
#include <numeric> // std::iota

using std::cout;
using std:: string;
using std::iota;

char * er(char* in) //sadly datastage really uses C for strings.
{
static string s;
s = in;
static unsigned char tbl[256];
static bool once = true;
if(once)
{
std::iota (tbl,tbl+256,0);
tbl['A'] = '!';
tbl['B'] = '@';
tbl['C'] = '#';
tbl['D'] = '$';
once = false;
}

for(int i = 0; i < s.length(); i++)
s = tbl[s];
return &s[0];
}

int main()
{
string s = "abcABCD123A!B!";
cout << er(&s[0]);
return 0;
}


output
C:\c>a
abc!@#$123!!@!
UCDI
Premium Member
Premium Member
Posts: 383
Joined: Mon Mar 21, 2016 2:00 pm

Post by UCDI »

The above, of course, is only doing 1 letter at a time. It isn't as slick to do string replacements; you can't use a lookup table for that as easily.

Here is the issue:
if you do a pure routine that is exactly like erep, then you have to use the failed logic of iterating the same string one time for each replacement string, whereas the above does them all at once. Again, that matters if you are replacing what you already replaced, but it is inefficient not matter what result you want.

Its faster if you custom make each replacer, so you have to decide if you want speed or a write once reusable block. Ive used a mix of both; I do have a datastage mimic ereplace in C++, and I have a few like the above, where it just knocks it out in one loop.

convert to c++ string wasn't needed here, but often you do want the string tools, so that was out of habit.
sarathchandrakt
Participant
Posts: 50
Joined: Fri Aug 29, 2014 1:32 pm
Location: Mumbai

Post by sarathchandrakt »

Used Convert and it worked. Then I came here to mention same and it was already in replies. Appreciate the help guys.
Post Reply