Field function
Moderators: chulett, rschirm, roy
Field function
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. :>
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
-
- Premium Member
- Posts: 385
- Joined: Wed Jun 16, 2004 12:43 pm
- Location: Virginia, USA
- Contact:
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]
Chuck Smith
www.anotheritco.com
www.anotheritco.com
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
-
- Premium Member
- Posts: 385
- Joined: Wed Jun 16, 2004 12:43 pm
- Location: Virginia, USA
- Contact:
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.
Chuck Smith
www.anotheritco.com
www.anotheritco.com
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
... but definitely do-able.
The fourth field is the one between the third and fourth occurrence of the delimiter. You need two Index() functions.
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.
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.
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]
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 ""
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.
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
-
- Premium Member
- Posts: 385
- Joined: Wed Jun 16, 2004 12:43 pm
- Location: Virginia, USA
- Contact:
Okay, try this in your stage variables:
This converts your "v." into field markers. Now it is a simple matter to use the field function to get your substrings.
Code: Select all
NewString = Change(YourString, "v.", @FM)
SubString1 = Field(NewString, @FM, 1)
SubString2 = Field(NewString, @FM, 2)
Chuck Smith
www.anotheritco.com
www.anotheritco.com
-
- Premium Member
- Posts: 385
- Joined: Wed Jun 16, 2004 12:43 pm
- Location: Virginia, USA
- Contact:
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.
Chuck Smith
www.anotheritco.com
www.anotheritco.com
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
-
- Premium Member
- Posts: 385
- Joined: Wed Jun 16, 2004 12:43 pm
- Location: Virginia, USA
- Contact:
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