Extracting data from String

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
dlvsrinivas
Participant
Posts: 18
Joined: Mon Jul 07, 2003 12:56 am
Location: India

Extracting data from String

Post by dlvsrinivas »

Hi,
I have the following requirement which I would like to represent using a BASIC routine.

A string contains data in name value pairs. The name is of type String and the value is of type numeric. Each name value pair is seperated by a blank. Now, I need to find the value stored for a given key(string).
Eg:
The main string will be in the following format.
"Offset=1 TransDate=1 Range=10"
Now I will pass a key as "Offset" and will expect 1 as result.

If any of you have similar function which is already prepared, could you please share the same? Pointers about the logic and BASIC functions are also welcome.

I had to check this option as it is very urgent for me and am pretty new to DataStage.

Thanks in advance.
Srinivas.
kduke
Charter Member
Charter Member
Posts: 5227
Joined: Thu May 29, 2003 9:47 am
Location: Dallas, TX
Contact:

Post by kduke »

Srinivas

The field command will parse this. Example:

tmp = "Offset=1 TransDate=1 Range=10"
Pair3 = field(tmp, " ", 3)
* Pair3 = "Range=10"
Name3 = field(Pair3, "=", 1)
* Name3 = "Range"
Value3 = field(Pair3, "=", 2)
* Value3 = 1

Kim.

Kim Duke
DwNav - ETL Navigator
www.Duke-Consulting.com
spracht
Participant
Posts: 105
Joined: Tue Apr 15, 2003 11:30 pm
Location: Germany

Post by spracht »

Srinivas

may the experts correct me, but to me it sounds like a case for a hashed file:
- manipulate your string so that each name/value pair appears on a separate line and write the result to a sequential file. In a separate job, read the sequential file and write it to a hashed file with the name as key.
- alternatively, you could create the hashfile using Basic (I think there are examples who to do this in other topics of this forum) and populate it something like this:

for i = 1 to DCount(string,' ')
name=field(field(string,' ',i),'=',1)
value=field(field(string,' ',i),'=',2)
rec=name:@FM:value
* write new record to hash
next i

Afterwards you would be able to get any value by using UtilityHashLookup, specifying the hashed file name, the key value and I think 2(position) as parameters.

Stephan
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Hah! [^]
If you only want one pairing resolved you don't even need to create a Routine. You could use a straightforward (!) output column derivation.
Let's imagine, though, that you want to maximize flexibility by creating a Routine - a transform function - to which is passed the entire string as the first argument, and the value name as the second argument.
The algorithm design is to search for the second argument in the first, then return that which follows the "=" sign (or return NULL if not found).
FUNCTION FindValue(Arg1, Arg2)
$COPYRIGHT "Copyright (c) 2003, Ray Wurlod. All rights reserved."

* Copy arguments to avoid pass-by-reference side effects
StringOfPairs = Arg1
ValueName = Arg2

* Determine location of ValueName in StringOfPairs
Location1 = Index(StringOfPairs, ValueName, 1)

* Return NULL if not found, otherwise return whatever is right of "="
If Location1 = 0
Then

Ans = @NULL

End
Else

* This assumes no spaces around the "=" sign; modify if needed.
SizeOfValueName = Len(ValueName)

* First substring argument is location of "=" sign.
RestOfString = StringOfPairs[Location1 + SizeOfValueName + 1, Len(StringOfPairs) - Location1]

* We want everything to the right of the "=" sign but to the left
* of the first space character. This does not handle quoted strings
* that include embedded spaces; modify if needed.
Ans = Field(RestOfString, " ", 1, 1)

End

RETURN(Ans)

Ray Wurlod
Education and Consulting Services
ABN 57 092 448 518
dlvsrinivas
Participant
Posts: 18
Joined: Mon Jul 07, 2003 12:56 am
Location: India

Post by dlvsrinivas »

Thanks a lot Ray.

The code you have given has worked absolutely according to my requirement without a single change.[:)] You have really saved a lot of my time.

Regards,
Srinivas.
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Does that mean you included my copyright notice? [8D]

All joking aside, the last couple of pieces of more complex code that I have posted have included such a notice, and I would appreciate it if people using it preserved that notice. The code is posted with the intention that you can use it, but would prefer that the fact that I am its original author to be recorded.

Ray Wurlod
Education and Consulting Services
ABN 57 092 448 518
Post Reply