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.
Routines to do EReplace Function
Moderators: chulett, rschirm, roy
-
- Participant
- Posts: 50
- Joined: Fri Aug 29, 2014 1:32 pm
- Location: Mumbai
-
- Participant
- Posts: 50
- Joined: Fri Aug 29, 2014 1:32 pm
- Location: Mumbai
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.
If anyone else have any ideas apart from routines, please feel free to mention.
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...
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...
Choose a job you love, and you will never have to work a day in your life. - Confucius
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!!@!
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!!@!
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.
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.
-
- Participant
- Posts: 50
- Joined: Fri Aug 29, 2014 1:32 pm
- Location: Mumbai