文字数の読み方は?
ラインの形式は次のとおりです。
vertices_count
X, Y
X, Y
X, Y
(X、Y のペアは同じ行にある可能性があります)
例えば:
3
12.5, 56.8
12.5, 56.8
12.5, 56.8
vertices_count 個の単語を読みたい (カンマをエスケープ):
したがって、上記の例では、単語の読み方は次のようになります。
12.5 56.8 12.5 56.8 12.5 56.8
文字数の読み方は?
ラインの形式は次のとおりです。
vertices_count
X, Y
X, Y
X, Y
(X、Y のペアは同じ行にある可能性があります)
例えば:
3
12.5, 56.8
12.5, 56.8
12.5, 56.8
vertices_count 個の単語を読みたい (カンマをエスケープ):
したがって、上記の例では、単語の読み方は次のようになります。
12.5 56.8 12.5 56.8 12.5 56.8
あなたが何を求めているのか、私にはまだはっきりとはわかりません。名前付きファイルからデータを読み取るコードを次に示します。他の質問から判断すると、入力ストリームに複数のデータセットを含めることができ、このコードはそれらすべてをリストとして返します。リストの各要素は 1 セットの座標です
# Read the input from file
set fil [open filename.file]
set input [read $fil]
close $fil
set data [list]; # No output so for
set seekCount yes; # Next token is a vertex count
foreach token [string map {, " "} $input] {
# Convert commas to spaces
if {$seekCount} {
set nCoords [expr $token * 2];
# Save number of coordinates
set datum [list]; # Clean out vertex buffer
} else {
lappend datum $token; # Save coordinate
incr nCoords -1
if {$nCoords <= 0} {
# That was the last coordinate
lappend data $datum; # Append the list of coordinates
set seekCount yes; # and look for anopther count
}
}
}
これは非常に手っ取り早い解決策であり、エラーを処理しようとはしません。ただし、対処できることの1つは、さまざまな量の空白と、コンマの後の空白の欠落です。
頑張ってください、これがお役に立てば幸いです。
このプロシージャは、最初に count 行を読み取り、次にその行数を読み取り、リストとして $varName に入れます。$varName の要素数を返すか、カウントが読み取られる前に EOF が発生した場合は -1 を返します。
proc getNLines {stream varName} {
upvar 1 $varName lines
set lines {}
if {[gets $stream n] < 0} {
return -1
}
while {$n > 0} {
if {[gets $stream line] < 0} {
error "bad data format"
}
lappend lines $line
incr n -1
}
return [llength $lines]
}
while {[getNLines stdin lines] >= 0} {
# ...
}
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