将列表传递给 proc tcl
Passing a list to a proc tcl
我已经看过 Passing list to Tcl procedure,但我不太明白如何正确地做到这一点。
在上下文中,这就是我传递列表的方式:
switch $choice {
n {
set ptable [new_partition {$ptable}]
}
d {
set ptable [delete_partition {$ptable}]
}
p {
set ptable [print_table {$ptable}]
}
w {
set ptable [write_table {$ptable $fn}]
puts "Saving file...\nExiting..."
break
}
q {
puts "Exiting..."
break
}
default {
puts "Illegal option"
}
}
这是程序之一的示例
proc print_table {ptable} {
# Set format string
set formatStr {%-16s%-8s%-8s%-8s%-8s%-8s}
# Print the contents of ptable to stdout
puts [format $formatStr "\nPartition" "Start" "End" "Blocks" "ID" "System"]
puts "--------------------------------------------------------"
foreach item $ptable {
set parts [lindex $item 0]
set sCyl [lindex $item 1]
set eCyl [lindex $item 2]
set blok [lindex $item 3]
set id [lindex $item 4]
set sys [lindex $item 5]
puts [format $formatStr $parts $sCyl $eCyl $blok $id $sys]
}
return $ptable
}
Ptable 正在正确创建,但是一旦我将它传递给其中一个过程,它就会丢失所有信息。我试过用 "{*}$ptable" 传递它,但它 returns 是一个错误。我程序中的其他所有内容都运行良好(如果我从任何一个过程中获取代码并将其单独放置,一切正常),我似乎无法让它正确地通过列表。
这里不要使用大括号:new_partition {$ptable}
-- 大括号禁止变量扩展,而你传递的是 7 个字符的字符串
$ptab le
参见http://tcl.tk/man/tcl8.6/TclCmd/Tcl.htm
中的规则#6
只需传递变量:new_partition $ptable
同样:
delete_partition $ptable
print_partition $ptable
write_partition $ptable $fn
您展示的 print_table
过程实际上并没有修改您传递给它的参数,因此您实际上不需要 return 一个值。
此外,如果您只是将 ptable 的行传递给 format
,则实际上不需要将它们分解为单个变量。你可以把那个过程变成
# Print the contents of ptable to stdout
proc print_table {ptable} {
set formatStr {%-16s%-8s%-8s%-8s%-8s%-8s}
puts [format $formatStr "\nPartition" Start End Blocks ID System]
puts "--------------------------------------------------------"
foreach item $ptable {
puts [format $formatStr {*}$item]
}
}
并且不要这样做
set ptable [print_table $ptable]
但是这样做
print_table $ptable
我已经看过 Passing list to Tcl procedure,但我不太明白如何正确地做到这一点。 在上下文中,这就是我传递列表的方式:
switch $choice {
n {
set ptable [new_partition {$ptable}]
}
d {
set ptable [delete_partition {$ptable}]
}
p {
set ptable [print_table {$ptable}]
}
w {
set ptable [write_table {$ptable $fn}]
puts "Saving file...\nExiting..."
break
}
q {
puts "Exiting..."
break
}
default {
puts "Illegal option"
}
}
这是程序之一的示例
proc print_table {ptable} {
# Set format string
set formatStr {%-16s%-8s%-8s%-8s%-8s%-8s}
# Print the contents of ptable to stdout
puts [format $formatStr "\nPartition" "Start" "End" "Blocks" "ID" "System"]
puts "--------------------------------------------------------"
foreach item $ptable {
set parts [lindex $item 0]
set sCyl [lindex $item 1]
set eCyl [lindex $item 2]
set blok [lindex $item 3]
set id [lindex $item 4]
set sys [lindex $item 5]
puts [format $formatStr $parts $sCyl $eCyl $blok $id $sys]
}
return $ptable
}
Ptable 正在正确创建,但是一旦我将它传递给其中一个过程,它就会丢失所有信息。我试过用 "{*}$ptable" 传递它,但它 returns 是一个错误。我程序中的其他所有内容都运行良好(如果我从任何一个过程中获取代码并将其单独放置,一切正常),我似乎无法让它正确地通过列表。
这里不要使用大括号:new_partition {$ptable}
-- 大括号禁止变量扩展,而你传递的是 7 个字符的字符串
$ptab le
参见http://tcl.tk/man/tcl8.6/TclCmd/Tcl.htm
中的规则#6只需传递变量:new_partition $ptable
同样:
delete_partition $ptable
print_partition $ptable
write_partition $ptable $fn
您展示的 print_table
过程实际上并没有修改您传递给它的参数,因此您实际上不需要 return 一个值。
此外,如果您只是将 ptable 的行传递给 format
,则实际上不需要将它们分解为单个变量。你可以把那个过程变成
# Print the contents of ptable to stdout
proc print_table {ptable} {
set formatStr {%-16s%-8s%-8s%-8s%-8s%-8s}
puts [format $formatStr "\nPartition" Start End Blocks ID System]
puts "--------------------------------------------------------"
foreach item $ptable {
puts [format $formatStr {*}$item]
}
}
并且不要这样做
set ptable [print_table $ptable]
但是这样做
print_table $ptable