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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TCL command

Status
Not open for further replies.

bebig

Technical User
Oct 21, 2004
111
US
I just started...100% beginner.

I am trying to find a data after open a .txt file.

here is an example

"111111","10/21/00",1,10,"10101",111,13

how to search or select these data.

10/21/00 and 111

is there any ways to count comma?


thank you
 
If I'm right about your question, you have read in a string from a text file. That string contains: "111111","10/21/00",1,10,"10101",111,13
quotes, commas, and all. Right so far?

I think the best thing for you at this point is to "split" the string into a list using the comma as the delimiter character.
Let's say you read a line from the file into a string variable, lineString. So the value of "lineString" is "111111","10/21/00",1,10,"10101",111,13.

Now, split lineString on commas and put the result in a list named lineList:
set lineList [split $lineString ,]

If you wanted to lose the "s, I'd do that first using "string map":
set lineString [string map {\" ""} $lineString]

Now, if you still want to know the number of commas in the original string, that's equal to the number of elements in the list minus 1:
expr {[llength $lineList]-1}

To find the index of the element whose value is, say, 10/21/00, use "lsearch":
set val1ix [lsearch $lineList 10/21/00]

To find the value of the 2nd element, use "lindex":
set val1 [lindex $lineList 1]

(note that lists are indexed from "0").

Bob Rashkin
rrashkin@csc.com
 
Thank you bob.
happy.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top