Tcl 如何阅读字数?
如何阅读字数 行具有以下格式:Tcl 如何阅读字数?,tcl,Tcl,如何阅读字数 行具有以下格式: vertices_count X, Y X, Y X, Y (X,Y对可以在同一行中) 例如: 3 12.5, 56.8 12.5, 56.8 12.5, 56.8 我想读一些单词(转义逗号): 因此,对于上述示例,阅读单词应该是: 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
我想读一些单词(转义逗号):
因此,对于上述示例,阅读单词应该是:
12.5 56.8 12.5 56.8 12.5 56.8
我还不清楚你到底想要什么。下面是一些从命名文件读取数据的代码。从您的另一个问题判断,您的输入流中可以有几组数据,而这段代码将它们全部作为列表返回。列表中的每个元素都是一组坐标
# 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
}
}
}
#从文件中读取输入
set fil[打开文件名.file]
设置输入[读取$fil]
收盘价$fil
设置数据[列表];#没有输出,所以
设置seekCount是;#下一个标记是顶点计数
foreach令牌[字符串映射{,“}$input]{
#将逗号转换为空格
如果{$seekCount}{
设置nCoords[expr$token*2];
#保存坐标数
设置基准[列表];#清除顶点缓冲区
}否则{
lappend datum$token;#保存坐标
增量NCORDS-1
如果{$nCoords我仍然不清楚您到底在找什么。下面是一些从命名文件读取数据的代码。从您的另一个问题判断,您的输入流中可以有几组数据,这些代码将它们全部作为列表返回。列表的每个元素都是一组坐标
# 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
}
}
}
#从文件中读取输入
set fil[打开文件名.file]
设置输入[读取$fil]
收盘价$fil
设置数据[列表];#没有输出以便
设置seekCount是;#下一个标记是顶点计数
foreach令牌[字符串映射{,“}$input]{
#将逗号转换为空格
如果{$seekCount}{
设置nCoords[expr$token*2];
#保存坐标数
设置基准[列表];#清除顶点缓冲区
}否则{
lappend datum$token;#保存坐标
增量NCORDS-1
如果{$NCoods
while{[llength$vertices]<$num*2}{
获取$fh行
foreach{ux y}[regexp-inline-all”($number\re),\\s*($number\re)“$line]{
lappend顶点$x$y
如果{[llength$顶点]=$num*2}中断
}
}
收盘价$fh
while{[llength$vertices]<$num*2}{
获取$fh行
foreach{ux y}[regexp-inline-all”($number\re),\\s*($number\re)“$line]{
lappend顶点$x$y
如果{[llength$顶点]=$num*2}中断
}
}
收盘价$fh
此过程首先读取计数行,然后读取该行数,并将其作为列表放入$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} {
# ...
}
proc getNLines{stream varName}{
upvar 1$varName行
设置行{}
如果{[获取$stream n]<0}{
返回-1
}
而{$n>0}{
如果{[获取$streamline]<0}{
错误“错误的数据格式”
}
lappend行$line
增量n-1
}
返回[llength$行]
}
而{[getNLines stdin line]>=0}{
# ...
}
此过程首先读取计数行,然后读取该行数,并将其作为列表放入$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} {
# ...
}
proc getNLines{stream varName}{
upvar 1$varName行
设置行{}
如果{[获取$stream n]<0}{
返回-1
}
而{$n>0}{
如果{[获取$streamline]<0}{
错误“错误的数据格式”
}
lappend行$line
增量n-1
}
返回[llength$行]
}
而{[getNLines stdin line]>=0}{
# ...
}
这对我来说有点太椭圆了。请明确说明一些典型输入和期望输出,以澄清您的问题?谢谢。这对我来说有点太椭圆了。请明确说明一些典型输入和期望输出,以澄清您的问题?谢谢。