Page 1 of 1

Extracting data from String

Posted: Fri Aug 08, 2003 8:51 am
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.

Posted: Fri Aug 08, 2003 9:12 am
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

Posted: Sun Aug 10, 2003 11:21 pm
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

Posted: Mon Aug 11, 2003 12:46 am
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

Posted: Mon Aug 11, 2003 5:26 am
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.

Posted: Mon Aug 11, 2003 5:31 am
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