tcl - How to read number count of words? -
how read number count of words?
lines has format:
vertices_count x, y x, y x, y
(x, y pair can in same line)
for example:
3 12.5, 56.8 12.5, 56.8 12.5, 56.8
i read vertices_count number of words(escaping comma):
so above example reading words should be:
12.5 56.8 12.5 56.8 12.5 56.8
set fh [open f r] gets $fh num read $fh data close $fh set number_re {-?\d+(?:\.\d*)?|-?\d*\.\d+} set vertices {} foreach {_ x y} [regexp -inline -all "($number_re),\\s*($number_re)" $data] { lappend vertices $x $y if {[llength $vertices] == $num * 2} break } puts $vertices # => 12.5 56.8 12.5 56.8 12.5 56.8
while {[llength $vertices] < $num * 2} { gets $fh line foreach {_ x y} [regexp -inline -all "($number_re),\\s*($number_re)" $line] { lappend vertices $x $y if {[llength $vertices] == $num * 2} break } } close $fh
Comments
Post a Comment