px version of Ereplace()

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

DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

px version of Ereplace()

Post by DSguru2B »

Hi Dxsians,
I wrote a px version of Ereplace(). It is not complete. I would say it is about 80% complete.
The following syntax is directly out of DS help

Code: Select all

Ereplace (string, substring, replacement [ ,number [ ,begin] ] )
The code that I prepared does everything except the "[ ,begin]" part. I developed this code on HP-UX. I ran and tested it out.

It has four input variables.
str is the string or expression.
subStr is the substring you want to replace.
rep is the replacement substring
num specifies the number of instances of substring to replace. To change all instances, use a value of 0.

Code: Select all

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

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

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

   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[i], rep);
          i += newlen;
          str += oldlen;
          num--;
       }
       else // if no match is found
       {
          result[i++] = *str++;
       }
    }
    else
    {
       result[i++] = *str++;
    }
   }

    result[i] = '\0'; //Terminate the string
    return result; //Return the replaced string
    free(result);   //free memory
}
You can use it, tweak it, change it to fit your needs.
Any suggestions are welcomed.
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
kumar_s
Charter Member
Charter Member
Posts: 5245
Joined: Thu Jun 16, 2005 11:00 pm

Post by kumar_s »

Awesome!!
Impossible doesn't mean 'it is not possible' actually means... 'NOBODY HAS DONE IT SO FAR'
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

It might be better to copy the first argument into a local variable, so that the original string (first input argument) is unaffected (as is the case with the BASIC Ereplace() 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.
DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

Post by DSguru2B »

Good suggestion. I am not exactly sure if its going to help but if I'll do that anyways, when i enhance it to handle the 'begin' part as well, sometime in the near future.
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

Post by DSguru2B »

Ok, i finally got the chance to complete it.

Code: Select all

#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[i] != '\0' ; i++)
   {
     if (strstr(&str[i], subStr) == &str[i])
     {
      count++;
      i += oldlen - 1;
      if (count == beg)
      {break;}
     }
   }

   //Get everything before position i before replacement begins

   x = 0;
   while (i != x)
   {  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
   
}
The only difference between this pxEreplace and Basice function Ereplace() is that pxEreplace requires all input variables.
EDIT:The variable that i added is beg
begspecifies the first instance to replace. A value less than 1 defaults to 1.
Last edited by DSguru2B on Fri Aug 17, 2007 2:22 pm, edited 3 times in total.
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
narasimha
Charter Member
Charter Member
Posts: 1236
Joined: Fri Oct 22, 2004 8:59 am
Location: Staten Island, NY

Post by narasimha »

Great job DSguru2B!
Narasimha Kade

Finding answers is simple, all you need to do is come up with the correct questions.
I_Server_Whale
Premium Member
Premium Member
Posts: 1255
Joined: Wed Feb 02, 2005 11:54 am
Location: United States of America

Post by I_Server_Whale »

Wonderful! :)
Anything that won't sell, I don't want to invent. Its sale is proof of utility, and utility is success.
Author: Thomas A. Edison 1847-1931, American Inventor, Entrepreneur, Founder of GE
us1aslam1us
Charter Member
Charter Member
Posts: 822
Joined: Sat Sep 17, 2005 5:25 pm
Location: USA

Post by us1aslam1us »

Awesome!!!!
kduke
Charter Member
Charter Member
Posts: 5227
Joined: Thu May 29, 2003 9:47 am
Location: Dallas, TX
Contact:

Post by kduke »

Not 2B

I loaded this up and it works great.

Thanks Kim.

P.S. Send me an email if Dallas sounds okay.
Mamu Kim
DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

Post by DSguru2B »

kduke wrote:Not 2B

I loaded this up and it works great.

Thanks Kim.

P.S. Send me an email if Dallas sounds okay.
Me not Kim :wink:
I didnt get your P.S. Sorry my brains are dead right now.
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
kduke
Charter Member
Charter Member
Posts: 5227
Joined: Thu May 29, 2003 9:47 am
Location: Dallas, TX
Contact:

Post by kduke »

You are looking for work in Texas. Is Dallas okay?
Mamu Kim
DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

Post by DSguru2B »

Dallas sounds great. But currently I am engaged in a project in NJ. Ill let you know once I am done here. So you are going to make me your disciple :wink:
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Slave.
-craig

"You can never have too many knives" -- Logan Nine Fingers
DSguru2B
Charter Member
Charter Member
Posts: 6854
Joined: Wed Feb 09, 2005 3:44 pm
Location: Houston, TX

Post by DSguru2B »

chulett wrote:Slave.
Maybe not.
Maybe a student.
On a serious note, I will definately let you know Kim once I am done with my current engagement. Thanks for looking out.
Regards,
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
kduke
Charter Member
Charter Member
Posts: 5227
Joined: Thu May 29, 2003 9:47 am
Location: Dallas, TX
Contact:

Post by kduke »

Harsh, Craig. Very harsh. He is NOT a 2B any more. He has arrived. No slave work for gurus.
Mamu Kim
Post Reply