TCl-Tk How to catch floating point numbers from a file -
i'm finding troubles working on file containing floating numbers. these rows file:
174259 1.264944 -.194235 4.1509e-5 174260 1.264287 -.191802 3.9e-2 174261 1.266468 -.190813 3.9899e-2 174262 1.267116 -.193e-3 4.2452e-2
what i'm trying find row desire number (e.g "174260") , extract following 3 numbers.
this code:
set output [open "output3.txt" w] set fileinput [open "input.txt" r] set filecontent [read $fileinput] set inputlist [split $filecontent "\n"] set desire 174260 set findelem [lsearch -all -inline $inputlist $desire*] set coordinate [ regexp -inline -all {\s+} $findelem ] set x1 [lindex $coordinate 1] set y1 [lindex $coordinate 2] set z1 [lindex $coordinate 3] puts $output "$x1 $y1 $z1"
using regexp method string "{\s+}" obtain last character curly brackets:
1.264287 -.191802 3.9e-2}
i don't know how extract numbers value , not entire string.
i'd tempted in case go simplest possible option.
set output [open "output3.txt" w] set fileinput [open "input.txt" r] set desire 174260 while {[gets $fileinput line] >= 0} { lassign [regexp -inline -all {\s+} $line] key x1 y2 z1 if {$key == $desire} { puts $output "$x1 $y1 $z1" } } close $fileinput close $output
failing that, problem you're using lsearch -all -inline
, returns list, , processing list string regexp
. should handle using:
foreach found $findelem { set coordinate [ regexp -inline -all {\s+} $found ] set x1 [lindex $coordinate 1] set y1 [lindex $coordinate 2] set z1 [lindex $coordinate 3] puts $output "$x1 $y1 $z1" }
this isn't understanding lines in first place, , working data 1 line @ time pretty trivial.
Comments
Post a Comment