PxEreplace routine issue when calling through datastage

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
krishna81
Premium Member
Premium Member
Posts: 78
Joined: Tue May 16, 2006 8:01 am
Location: USA

PxEreplace routine issue when calling through datastage

Post by krishna81 »

Hi i tried below routine which suppose to replace string with given substring but and i have an issue with this.i am ble to process less volune of records(ex:1000) and when i ran with huge volume(ex:15 million)of records it is hanging and keep running never finished and no warnings.is there any buffer issue in this c program
i appreciate in advance for your valuble suggestions.


#include "stdio.h"
#include "string.h"
#include "stdlib.h"

char* pxEreplace(char *str, char *subStr, char *rep, int num, int beg)
{
char *result = (char *)malloc (sizeof(char *));
int newlen = strlen(rep);
int oldlen = strlen(subStr);
int i, x, count = 0;

//If begining is less than or equal to 1 then default it to 1
if (beg <= 1)
{beg = 1;}

//replace all instances if value of num less than or equal to 0
if (num <= 0)
{num = strlen(str);}

//Get the character position in i for substring instance to start from
for (i = 0; str != '\0' ; i++)
{
if (strstr(&str, subStr) == &str)
{
count++;
i += oldlen - 1;
if (count == beg)
{break;}
}
}

//Get everything before position i before replacement begins

x = 0;
while (i == x) //replace from starting//
{ result[x++] = *str++; }

//Start replacement
while (*str) //for the complete input string
{

if (num != 0 ) // untill no more occurances need to be changed
{
if (strstr(str, subStr) == str )
{
strcpy(&result[x], rep);
x += newlen;
str += oldlen;
num--;
}
else // if no match is found
{
result[x++] = *str++;
}
}
else
{
result[x++] = *str++;
}
}

result[x] = '\0'; //Terminate the string
return result; //Return the replaced string
}
Datastage User
krishna81
Premium Member
Premium Member
Posts: 78
Joined: Tue May 16, 2006 8:01 am
Location: USA

pxEreplace issue

Post by krishna81 »

can any post your thoughts here.
Datastage User
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Patience. If anyone has any, they will.
-craig

"You can never have too many knives" -- Logan Nine Fingers
Sainath.Srinivasan
Participant
Posts: 3337
Joined: Mon Jan 17, 2005 4:49 am
Location: United Kingdom

Post by Sainath.Srinivasan »

Initial looks suggest possible memory allocation problem due to malloc.

Try setting that as result[1000] or your maximum string length.

Alternatively you can use another variable at the end to hold the return value and free the result.

Posted same in FAQ also.
krishna81
Premium Member
Premium Member
Posts: 78
Joined: Tue May 16, 2006 8:01 am
Location: USA

resolved

Post by krishna81 »

it worked for men now for huge records by removing malloc and replaced with dynamic array.

#include "iostream.h"
#include "string.h"
#include "stdlib.h"

char* PrStrRep( char *InStr, char *SubStr, char *Replace, int Occurance, int Starting)
{

char *result = new char[2000];
size_t newlen = strlen(Replace);
size_t oldlen = strlen(SubStr);
int i, x, count = 0;

so on ...
Datastage User
kduke
Charter Member
Charter Member
Posts: 5227
Joined: Thu May 29, 2003 9:47 am
Location: Dallas, TX
Contact:

Post by kduke »

Why not post the complete source so others can benefit. Please put code tags around it.
Mamu Kim
krishna81
Premium Member
Premium Member
Posts: 78
Joined: Tue May 16, 2006 8:01 am
Location: USA

as per duke's suggestion

Post by krishna81 »

Here is the complete code

#include "iostream.h"
#include "string.h"
#include "stdlib.h"

char* PrStrRep( char *InStr, char *SubStr, char *Replace, int Occurance, int Starting)
{

char *result = new char[2000];
size_t newlen = strlen(Replace);
size_t oldlen = strlen(SubStr);
int i, x, count = 0;
//If begining is less than or equal to 1 then default it to 1
if (beg <= 1)
{beg = 1;}

//replace all instances if value of num less than or equal to 0
if (num <= 0)
{num = strlen(str);}

//Get the character position in i for substring instance to start from
for (i = 0; str != '\0' ; i++)
{
if (strstr(&str, subStr) == &str)
{
count++;
i += oldlen - 1;
if (count == beg)
{break;}
}
}

//Get everything before position i before replacement begins

x = 0;
while (i == x) //replace from starting//
{ result[x++] = *str++; }

//Start replacement
while (*str) //for the complete input string
{

if (num != 0 ) // untill no more occurances need to be changed
{
if (strstr(str, subStr) == str )
{
strcpy(&result[x], rep);
x += newlen;
str += oldlen;
num--;
}
else // if no match is found
{
result[x++] = *str++;
}
}
else
{
result[x++] = *str++;
}
}

result[x] = '\0'; //Terminate the string
return result; //Return the replaced string
}
Datastage User
gowrishankar_h
Participant
Posts: 42
Joined: Wed Dec 26, 2012 1:13 pm

Post by gowrishankar_h »

Thanks for the code.

substring replace is working when the substring is present in middle of the string.but its not working when it is the starting word of the string.

Example:

Replace substring which is in middle of the string

Source before replace: ETL is good tool
Doing replace good to very good
Target after replace :ETL is very good tool


Replace substring which is starting word of a string

Source before replace: ETL is good tool
Doing replace ETL to Datastage
Target after replace :ETL is good tool

In the above scenario its not working... Not able to find where its breaking.Please help me to fix this.


Thanks,
Gowri Shankar H
priyadarshikunal
Premium Member
Premium Member
Posts: 1735
Joined: Thu Mar 01, 2007 5:44 am
Location: Troy, MI

Post by priyadarshikunal »

A resolved topic doesn't get much attention, so you should start a new topic and link back to this post.

Did you try the one posted by Phil Hibbs with the changes from malloc to new as done by Krishna?

See the last post for the code.

viewtopic.php?t=106358&postdays=0&posto ... e&start=30
Priyadarshi Kunal

Genius may have its limitations, but stupidity is not thus handicapped. :wink:
gowrishankar_h
Participant
Posts: 42
Joined: Wed Dec 26, 2012 1:13 pm

Post by gowrishankar_h »

Thanks a lot...It worked for me 8)

Issue is with the below code

Code:
if (strstr(&str, subStr) == &str)
{
count++;
i += oldlen - 1;
if (count == beg)
{break;}
}


I changed it to this:
Code:
if (strstr(&str, subStr) == &str)
{
count++;
if (count == beg)
{break;}
i += oldlen - 1;
}
qt_ky
Premium Member
Premium Member
Posts: 2895
Joined: Wed Aug 03, 2011 6:16 am
Location: USA

Post by qt_ky »

Or you can upgrade to 9.x or higher and use the built in ereplace function.
Choose a job you love, and you will never have to work a day in your life. - Confucius
priyadarshikunal
Premium Member
Premium Member
Posts: 1735
Joined: Thu Mar 01, 2007 5:44 am
Location: Troy, MI

Post by priyadarshikunal »

Good that you got it resolved. This bug was already identified by Phil, may not be the same scenario but he has it fixed in his code posted.
Priyadarshi Kunal

Genius may have its limitations, but stupidity is not thus handicapped. :wink:
Post Reply