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

getting the strings from a line 1

Status
Not open for further replies.

RajVerma

Programmer
Jun 11, 2003
62
DE
hi all, I have a data file which has some lines of data. If I want to get the Yth string of the Xth line of this data file into my text box when I open the tk file, wat shud I do after opening the read channel? thanq.

Raj.
 
hi all, well I'm answering my own query as I got the thing working. the code will look like this.

set file [open $path r]
foreach line [split [read $file] \n] {
set index [string range [lindex $line 0] 0 end]
set freq1 [string range [lindex $line 1] 0 end]
set time1 [string range [lindex $line 2] 0 end]
}
close $file

where lindex gives the number of the nth string of the line in that particular loop, and range gives us the exact string that we wanted.

cheers,
Raj.
 
By the way, there's no need for the string range commands in your code there:

Code:
set file [open $path r]
foreach line [split [read $file] \n] {
    set index [lindex $line 0]
    set freq1 [lindex $line 1]
    set time1 [lindex $line 2]
}
close $file

Note that your trick is a fine way to parse data, and frequently used in Tcl. However, it relies on the data being in proper Tcl list format. If one of the lines turns out not to be a valid Tcl list (for example, containing unbalanced quoting characters), lindex raises an error. So, make sure that your data is properly formatted, or you'll run into trouble.

An alternative, in case you can't guarantee the format of the data, is to use a package provided by Tcllib, the Standard Tcl Library. The Tcl'ers Wiki ( has more information about Tcllib at Tcllib includes a text processing extension called textutil, which contains a splitx command for splitting a string into a list based on arbitrary delimiter patterns that you provide. The default delimiter is any whitespace sequence. Thus, you could always safely do the following:

Code:
package require textutil
set file [open $path r]
foreach line [split [read $file] \n] {
    set line [textutil::splitx $line]
    set index [lindex $line 0]
    set freq1 [lindex $line 1]
    set time1 [lindex $line 2]
}
close $file

or use the equivalent bit of Tcl trickery to "distribute" the elements of a list into multiple variables:

Code:
package require textutil
set file [open $path r]
foreach line [split [read $file] \n] {
    foreach {index freq1 time1} [textutil::splitx $line] {
        break
    }
}
close $file

- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top