Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

How can I parse comma or tab delimited data using SilkTest?

How can I parse comma or tab delimited data using SilkTest?

If you have a string "hello, yellow balloon, red", you may want to extract each element of the string like this:
hello
yellow balloon
red

Here are two possible ways to achieve this:
1- You can use the GetField() function to return a segment (field) of a string.

sSegment = GetField (sString, sDelim, iField)

Variable Description
sSegment The returned segment. STRING.
sString The string to return a segment from. STRING.
sDelim The character(s) to use to separate fields in sString. STRING.
iField The field to return. INTEGER.
Notes

Use GetField to return segments of strings that have a known, consistent format (like part numbers or telephone numbers). GetField treats a string like a record; it uses the delimiter you specify to logically separate the string into fields. If the string has several segments that are separated by the same delimiter, use iField to get the part you want.
GetField returns an empty string ("") when the field is not found.

Example

STRING sData = "name|rank|serial#"
Print (GetField (sData, "|", 1) ) // prints: name
Print (GetField (sData, "|", 3) ) // prints: serial#
Print (GetField (sData, "|", 4) ) // prints:

2-You can also use the function below which has a similar output to GetField but is used in a loop (this is an example and may need modified to suit your specific needs).

[-] LIST OF STRING LikeSplit(STRING s, STRING eD)
    [ ] list of string lsText
    [ ] string sSubString, sChanged
    [ ] INTEGER i, iPos, iLength, iSubString
    [ ] //get the length of the string
    [ ] iLength=Len(s)
    [ ] //if the delimiter is a , or a /
    [-] if (eD == ",") || (eD == "/")
        [ ] //loop the length of the string
        [-] for i = 1 to iLength
            [ ] //if the delimiter is find in the string
            [-] if (MatchStr("*{eD}*", s)==true)
                [ ] 
                [ ] //start parsing the string
                [ ] iPos = StrPos(eD, s)
                [ ] iSubString = iPos - 1
                [ ] sSubString = Left(s, iSubString)
                [ ] 
                [ ] sChanged = Stuff(s,iPos, 1, "")
                [ ] s = Right(sChanged,iLength-iPos)
                [ ] 
                [ ] Trim(sSubString)
                [ ] 
                [ ] //add the parsed string to a list
                [ ] ListAppend(lsText, sSubString)
                [ ] sChanged=""
                [ ] iLength=Len(s)
        [ ] Trim(s)
        [ ] ListAppend(lsText, s)
    [-] else
        [ ] ListAppend(lsText, s)
    [ ] return lsText
[-] testcase test1() appstate none
    [ ] string ss = "hello , yellow , red"
    [ ] string stab = "snow / brown / black"
    [ ] string stext = " hello tuesday"
    [ ] listprint(LikeSplit(ss, ","))
    [ ] listprint(LikeSplit(stab, "/"))
    [ ] listprint(LikeSplit(stext, "/"))

Results:
[-] Testcase test1 - Passed
[ ] hello
[ ] yellow
[ ] red
[ ] snow
[ ] brown
[ ] black
[ ] hello tuesday

Old KB# 21576

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2013-02-15 19:22
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.