Saturday, October 24, 2015

Anything iCanDo, You Can Do Better

Have to give these guys credit for sticking around so long - probably been 20 years..

http://icanprogram.com/09tk/lesson6/lesson6.html

Their recipe for displaying the contents of a text field :

proc saveFile { } {
set fn "saveFile"
global f

set myFile [tk_getSaveFile]

puts stdout [format "%s:myFile=<%s>" $fn $myFile]

set line [$f.t get 1.0 1.end]
for {set i 2} {[string length $line] > 0 } {incr i } {
puts stdout [format "line=<%s>" $line]
set line [$f.t get $i.0 $i.end]
} ;#end for loop
} ;#end saveFile 

Here's my tweak that's much more elegant (off the web of course) :

set nLines [$f.t count -lines 1.0 end]
for {set i 1} { $i <= $nLines } {incr i } {
set line [$f.t get $i.0 $i.end]
puts stdout [format "line : %s" $line]
} ;#end for loop
} ;#end saveFile

No comments: