Replacing character pairs

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

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

Replacing character pairs

Post by chulett »

I have a field I need to strip pairs of characters from, basically any place I find a caret ^ in the string I need to remove it and the immediately following character. Problem is it's not a small list like only '^R' and '^C', there are 90 different values so something simple like EReplacing them with an empty string isn't feasible.

I know I can find out how many occurances there are in the string with Count and then interate through each chunk using Index, building another copy of the string with these pairs left out... or something like that, anyways. However, before embarking on that boat...

I was wondering if there is a 'better' way to do this, perhaps with Match or something similar? Not having much opportunity to use it yet, I'm not all that familiar with the goofy "0N0X5N..." style pattern matching it needs. :?

I would love to find a single pass way to say - "find every place a caret exists in this string and remove it and the next character". Any way to do this in one swell foop? :wink:
-craig

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

Post by ray.wurlod »

String handling functions are way fast, so a custom routine is probably the easiest way to go.

Code: Select all

FUNCTION ReplaceCharacterPairs(TheString)
If UnAssigned(TheString) Or IsNull(TheString)
Then
   RETURN(@NULL)
End
Ans = ""
Size = Len(TheString)
For Pos = 1 To Size
   TheChar = TheString[Pos,1]
   If TheChar = "^"
   Then
      Pos += 1
   End
   Else
      Ans := TheChar
   End
Next Pos
RETURN(Ans)
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Good to know they are 'way fast'. :wink:

I was thinking about cutting the string up around the pairs and putting the chunks together, but if iterating through character by character is speedy - you're right, that seems the simplest approach. 8)
-craig

"You can never have too many knives" -- Logan Nine Fingers
Post Reply