Anonymous user
Optional parameters: Difference between revisions
→{{header|Tcl}}: improve the satisfaction of the task by the examples
(added Ursala) |
(→{{header|Tcl}}: improve the satisfaction of the task by the examples) |
||
Line 404:
=={{header|Tcl}}==
Tcl supports optional parameters to procedures through two mechanisms. It can either work positionally (through giving default values for arguments) or by using a special last argument called “<code>args</code>” which will collect all the remaining arguments into a list that can be processed by the procedure.
{{works with|Tcl|8.4}}
<lang Tcl>package require Tcl 8.5▼
set direction [expr {$reverse ? "-decreasing" : "-increasing"}]
if {$ordering ne ""} {
lsort -command $ordering $direction -index $column $table
} else {
lsort $direction -index $column $table
}
}
puts [tablesort $data "" 0 1]
puts [tablesort $data {
apply {{a b} {expr {[string length $a]-[string length $b]}}}
}]</lang>
When using the second style, it is often common to use [[Named Arguments]] (and in fact the “<code>lsort</code>” already works very much like this). Note that it is most common to use named arguments that start with a “<code>-</code>”, but we omit them here so that we formally match the requirements of the task.
<br>
{{works with|Tcl|8.5}}
▲<lang Tcl>package require Tcl 8.5; # Only for the list expansion syntax
proc tablesort {table args} {
array set opt {
array set opt $args
set pars [list -index $opt(
if {$opt(
if {$opt(
lsort {*}$pars $table
}
puts [tablesort $data]
puts [tablesort $data column 1]
puts [tablesort $data column 0]
puts [tablesort $data
puts [tablesort $data ordering {
▲puts [tablesort $data -column 0]
▲puts [tablesort $data -column 0 -reverse 1]
▲puts [tablesort $data -ordering {
apply {{a b} {expr {[string length $b]-[string length $a]}}}
}]</lang>
=={{header|Ursala}}==
|