Code Comments
Programming Forum and web based access to our favorite programming groups.Hi I am new to Tcl. I need to do very simple stuff. Cat a file that has 100 lines and every line has two fields. I need to cat the file one line at a time, and take the value of the fields in two varaibles. Do some processing and then when file hits EOF quit. Any comments will be appreciated. Thanks Nitin
Post Follow-up to this messageto simple 1.open the file set file [ open filename r ] 2.read the whole file set str [read $file] 3.split str with "\n" or "\r" newline character 4.foeach loop will help you to access whole line ( that has 2 elements ) Abhishek
Post Follow-up to this messageabhishekkabra@gmail.com wrote: # to simple # 1.open the file set file [ open filename r ] 1.5. fconfigure $file -translation (whatever) # 2.read the whole file set str [read $file] # 3.split str with "\n" or "\r" newline character # 4.foeach loop will help you to access whole line ( that has 2 elements The translation option can be used to convert \r, \n, or \r\n into \n during all input and output operations. -- SM Ryan http://www.rawbw.com/~wyrmwif/ I'm not even supposed to be here today.
Post Follow-up to this messageNitin wrote: > I am new to Tcl. I need to do very simple stuff. Cat a file that has > 100 lines and every line has two fields. I need to cat the file one > line at a time, and take the value of the fields in two varaibles. Do > some processing and then when file hits EOF quit. This feels a little bit like homework, so I won't give a full solution. I'll just point to the pieces you need. To read the file in, you'll want [open], [read] and [close]. Then you'll want to [split] the data by newlines (100 lines is a small file) and iterate over the list of lines using [foreach]. You will probably want to parse each line with [split] or [regexp] (choosing which to use depends on the detailed format of the data being read). Alternatively, you could use [open], [while], [eof], [gets], parse the line that you've read (see notes above) and put [close] after the loop. You might or might not find helpful examples by looking on our wiki, which is nicely indexed by google BTW. ;-) Donal.
Post Follow-up to this messageDonal K. Fellows wrote: > > Alternatively, you could use [open], [while], [eof], [gets], parse the > line that you've read (see notes above) and put [close] after the loop. This is the fastest option of the two :^) Khaled
Post Follow-up to this messageIn article <1118049979.883757.307600@o13g2000cwo.googlegroups.com>, Khaled <nospam.ksubs@free.fr> wrote: > > >Donal K. Fellows wrote: > >This is the fastest option of the two :^) > >Khaled > Not always. It depends on specifics of the execution environment and data.
Post Follow-up to this messageIn article <11a7su22bqpjheb@corp.supernews.com>, SM Ryan <wyrmwif@tango-sierra-oscar-foxtrot-tango.fake.org> wrote: >abhishekkabra@gmail.com wrote: ># to simple ># 1.open the file set file [ open filename r ] > >1.5. fconfigure $file -translation (whatever) > ># 2.read the whole file set str [read $file] ># 3.split str with "\n" or "\r" newline character ># 4.foeach loop will help you to access whole line ( that has 2 elements > >The translation option can be used to convert \r, \n, or \r\n into \n durin g >all input and output operations.
Post Follow-up to this messageOn Mon, 6 Jun 2005, Cameron Laird wrote:
> Indeed it can--but to what purpose that bears on the original requirements
?
>
> I'm tempted to submit
>
> # *This* certainly is fragile in the face of exceptions.
> foreach {first second} [split [exec cat $filename] \n] {
> puts "The two items are '$first' and '$second'."
> }
>
My first reaction would be:
set fp [open $filename]
while {[gets $fp line] != -1} {
foreach {first second} $line break
# do whatever
}
close $fp
or, as a pure-Tcl alternative to [exec cat]:
package require fileutil
foreach {first second} [split [fileutil::cat $filename] \n] {
# do whatever
}
Michael
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.