Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read a large file line by line using tcl?

Tags:

file

tcl

I've written one piece of code by using a while loop but it will take too much time to read the file line by line. Can any one help me please? my code :

   set a [open myfile r]              
   while {[gets $a line]>=0} {   
     "do somethig by using the line variable" 
   }
like image 438
pawankalyan Avatar asked Jun 30 '15 13:06

pawankalyan


2 Answers

The code looks fine. It's pretty quick (if you're using a sufficiently new version of Tcl; historically, there were some minor versions of Tcl that had buffer management problems) and is how you read a line at a time.

It's a little faster if you can read in larger amounts at once, but then you need to have enough memory to hold the file. To put that in context, files that are a few million lines are usually no problem; modern computers can handle that sort of thing just fine:

set a [open myfile]
set lines [split [read $a] "\n"]
close $a;                          # Saves a few bytes :-)
foreach line $lines {
    # do something with each line...
}
like image 162
Donal Fellows Avatar answered Sep 17 '22 15:09

Donal Fellows


If it truly is a large file you should do the following to read in only a line at a time. Using your method will read the entire contents into ram.

https://www.tcl.tk/man/tcl8.5/tutorial/Tcl24.html

#
# Count the number of lines in a text file
#
set infile [open "myfile.txt" r]
set number 0

#
# gets with two arguments returns the length of the line,
# -1 if the end of the file is found
#
while { [gets $infile line] >= 0 } {
    incr number
}
close $infile

puts "Number of lines: $number"

#
# Also report it in an external file
#
set outfile [open "report.out" w]
puts $outfile "Number of lines: $number"
close $outfile
like image 34
Todd Horst Avatar answered Sep 21 '22 15:09

Todd Horst