Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Line String Tokens

Status
Not open for further replies.

hankm3

Programmer
Jan 27, 2002
284
0
0
US
Hello,

Would someone push me in the right Direction. I have a Line String and need to extract only certain fields (Tokens) from the Line String. Have been trying to use STRTOK; but the Procomm 4.8 Examples did not make it clear.

My Example:

555-1212 555-1212 0010231931G 4 Tip/Ring Open Failed
555-1213 555-1213 0010231912E 3 Tip/Ring Open Failed
555-1214 555-1214 0020241831G 2 Tip/Ring OK Passed
555-1215 555-1214 0050211945A 1 ADSL OK Passed

All fields are seperated by 1 or more Spaces and there may or not be a Forward Slash. I only want to extract the 1st 2nd and 6th Fields.

Thanks

Hank
 
Here is a modified version of the example script for the strtok command. I replaced the text string in the file with the first line of your data. The delimiter I used was a space (the " " parameter associated with the strtok command). It doesn't matter if there are one or two space between the different bits of data, as long as there is always at least one space. In your particular case, you will want to replace the usermsg command with something else (probably assigning the extracted data to another string).

proc main
string DataStr ; String to contain data.
string ItemStr ; String to contain extracted item.

DataStr = "555-1212 555-1212 0010231931G 4 Tip/Ring Open Failed" ; Assign hex values to data list.
while 1 ; Loop forever, or until EXITWHILE.
strtok ItemStr DataStr " " 1 ; Get first item from data list.
if nullstr ItemStr ; Check to see if item is null.
exitwhile ; If so, exit loop.
endif

usermsg "Data item is `"%s`"." ItemStr
endwhile
endproc
 
Thanks Knob,

The Data is in a Text File and is not Declared. I tried using the S0 - S9 System Variables and was able to Grab Fields 1 and 2 ; But after that everything Field did not Extract. I played with it today and was able to Extract the Fields. Crude way of doing it but it did work. There are 8 Fields per Line.

while not feof 1
fgets 1 sLine
strtok sTok1 sLine " " 1 ; sTok1 = Token 1
strtok sTok2 sLine " " 1 ; sTok2 = Token 2
strtok sTok3 sLine " " 1 ; sTok2 = Token 3
strtok sTok4 sLine " " 1 ; sTok2 = Token 4
strtok sTok5 sLine " " 1 ; sTok2 = Token 5
strtok sTok6 sLine " " 1 ; sTok2 = Token 6
strtok sTok7 sLine " " 1 ; sTok2 = Token 7
strtok sTok8 sLine " " 1 ; sTok2 = Token 8
termwrites sTok1 ; Print Token 1 to Screen
termwrites " " ; Space between s1 and s2
termwrites sTok8 ; Print Token 8 to Screen
endwhile

Thanks
Hank
 
Hello.

I think you've solved your problem but I just felt like adding a comment. The function "strextract" is another useful funtion to do string extracts. This function also has the advantage that it doesn't alter the origin string. That is the situation with "strtok". An example to extract the 1st, 2nd, and 6th substring could look like this:

strextract sOne sLine " " 0 ; First item
strextract sTwo sLine " " 1 ; Second item
strextract sSix sLine " " 5 ; Sixth item

Note that indexes are zero-based.

Best regards.
 
liebknecht

I tried what you suggested but I still only got Token 0. This is what I used. If you see the Bug. Please let me know.

Thank You for your help !
Hank

proc main
string sLine, sOne, sThr, sSev
clear
fopen 1 "C:\DMS 100\OpenShort.txt" READ TEXT
while not feof 1
fgets 1 sLine
strextract sOne sLine " " 0 ; sTok0 = Token 0
strextract sThr sLine " " 3 ; sTok3 = Token 3
strextract sSev sLine " " 7 ; sTok7 = Token 7
termwrites sOne ; Print Token 0 to Screen
termwrites " " ; Spaces between sTok1 and sTok4
termwrites sThr ; Print Token 3 to Screen
termwrites " " ; Space between sTok4 and sTok8
termwrites sSev ; Print Token 7 to Screen
termwrites "`n`r"
endwhile
fclose 1
usermsg "Finished"
endproc
 
Unlike strtok, strextract looks for the exact number of spaces between elements that you specified, in this case only one space. To use strextract, you will have to know how many spaces are between each element of the string. Also, if the number of spaces is not consistent between the elements (as in your case), then you will run into some problems as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top