Field function

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
EJRoufs
Participant
Posts: 73
Joined: Tue Aug 19, 2003 2:12 pm
Location: USA

Field function

Post by EJRoufs »

I have a string of 255 bytes that we want to split into 2 separate strings. The place we want to split them within is before and after a "v.".

I am using the "Field" function to try to do this. The problem is this. My delimiter is 2 characters long instead of 1. It doesn't seem to like that. I am setting it up as:

Field(ToTransform.TestString, 'v.', 1, 1)
Field(ToTransform.TestString, 'v.', 2, 1)

to retrieve off the 1st and 2nd parts of the string and send them to different fields. It seems to just use the "v" as the delimiter, instead of the "v.". Does anyone know of a work-around for this problem?

Thanks in advance. :>
Eric
chucksmith
Premium Member
Premium Member
Posts: 385
Joined: Wed Jun 16, 2004 12:43 pm
Location: Virginia, USA
Contact:

Post by chucksmith »

Use the index function to find the "v.", then use [] to retrieve each substring.

Code: Select all

ptr = Index(YourString,"v.",1)
SubString1 = YourString[1,(ptr - 1)]
SubString2 = YourString[Len(YourString) - ptr - 1]
EJRoufs
Participant
Posts: 73
Joined: Tue Aug 19, 2003 2:12 pm
Location: USA

Post by EJRoufs »

chucksmith wrote:Use the index function to find the "v.", then use [] to retrieve each substring.

Code: Select all

ptr = Index(YourString,"v.",1)
SubString1 = YourString[1,(ptr - 1)]
SubString2 = YourString[Len(YourString) - ptr - 1]

It looks like you're assigning a variable (PTR) that i can use in my Transformer Stage. Is that true? I have not seen a place to do that within the Transformer Stage. Where is that done?
Eric
chucksmith
Premium Member
Premium Member
Posts: 385
Joined: Wed Jun 16, 2004 12:43 pm
Location: Virginia, USA
Contact:

Post by chucksmith »

The easiest way is to create stage variables in your transform stage. Name them ptr, SubString1, and SubString2. The code to the right of the "=" is your derivation for each stage variable.
kduke
Charter Member
Charter Member
Posts: 5227
Joined: Thu May 29, 2003 9:47 am
Location: Dallas, TX
Contact:

Post by kduke »

Chuck is correct but index() will also find the second or third or nth occurance of a string. It is a little harder to get the equivelant of field 4 because you need to find where field 3 ends and then where field 4 ends.
Mamu Kim
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

... but definitely do-able.

The fourth field is the one between the third and fourth occurrence of the delimiter. You need two Index() functions.

Code: Select all

TheString[Index(TheString,"v.",3)+1,Index(TheString,"v.",4)-1]
This is not a complete solution, as it does not handle the case that there are only three delimiters. You need to wrap the thing in some If..Then..Else logic to ensure that both delimiters exist. Return "" if the third does not exist. Return from the third to the end of the string if the third exists but the fourth does not.

Code: Select all

If Index(TheString,"v.",3) Then If Index(TheString,"v.",4) Then TheString[Index(TheString,"v.",3)+1,Index(TheString,"v.",4)-1 Else TheString[Index(TheString,"v.",3)+1,Len(TheString)] Else ""
If you evaluate the two Index functions in stage variables, you can avoid evaluating them twice. You could also make the technique more generic if the delimiter were a stage variable too.
Or construct the whole thing as a totally re-usable component (Routine) with the delimiter, start and end positions as arguments. Maybe call the routine LongDelimField.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
chucksmith
Premium Member
Premium Member
Posts: 385
Joined: Wed Jun 16, 2004 12:43 pm
Location: Virginia, USA
Contact:

Post by chucksmith »

Okay, try this in your stage variables:

Code: Select all

NewString = Change(YourString, "v.", @FM)

SubString1 = Field(NewString, @FM, 1)

SubString2 = Field(NewString, @FM, 2)

This converts your "v." into field markers. Now it is a simple matter to use the field function to get your substrings.
chucksmith
Premium Member
Premium Member
Posts: 385
Joined: Wed Jun 16, 2004 12:43 pm
Location: Virginia, USA
Contact:

Post by chucksmith »

In Ray's multi-Index() example, since the delimiting string is two bytes long, the +1 should be +2 to point to the actual start of the data.
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Nice catch. I couldn't see the dot because of the tiny font (would you believe)?
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
chucksmith
Premium Member
Premium Member
Posts: 385
Joined: Wed Jun 16, 2004 12:43 pm
Location: Virginia, USA
Contact:

Post by chucksmith »

I believe. I share the same problem.
EJRoufs
Participant
Posts: 73
Joined: Tue Aug 19, 2003 2:12 pm
Location: USA

Post by EJRoufs »

chucksmith wrote:The easiest way is to create stage variables in your transform stage. Name them ptr, SubString1, and SubString2. The code to the right of the "=" is your derivation for each stage variable.

I ended up going this route..... creating a Stage Variable and using the Index command. It works wonderfully, and is very simple! Thanks! And thanks to everybody else for all the ideas, as well. :>
Eric
Post Reply