Reputation: 25
I already looked at Passing list to Tcl procedure, and I couldn't quite understand how to do it properly. To put it in context, this is how I'm passing the list:
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"
}
}
This is an example of one of the procedures
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 is being created correctly, but it loses all its information as soon as I pass it to one of the procedures. I've tried passing it with "{*}$ptable" but it returns an error. Everything else in my program is working perfectly (if I take the code from any single procedure and put it by itself, it all works), I just can't seem to get it to pass the list properly.
Upvotes: 1
Views: 1313
Reputation: 246744
Don't use braces here: new_partition {$ptable}
-- braces inhibit variable expansion, and you are passing the 7-character string
$ptable
See rule #6 in http://tcl.tk/man/tcl8.6/TclCmd/Tcl.htm
Just pass the variable: new_partition $ptable
Similarly:
delete_partition $ptable
print_partition $ptable
write_partition $ptable $fn
The print_table
procedure you've shown does not actually modify the argument you pass to it, so you don't really need to return a value.
Also, you don't really need to decompose the rows of ptable into individual variables if you're just passing them to format
. You can turn that proc into
# 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]
}
}
And don't do this
set ptable [print_table $ptable]
but do this
print_table $ptable
Upvotes: 1