Jump to content

99 Bottles of Beer/Tcl: Difference between revisions

m
order of versions
(moved code from main task-page to sub-page)
m (order of versions)
Line 1:
{{collection|99 Bottles of Beer}} [[implementation of task::99 Bottles of Beer| ]]
 
===using variable traces===
Here's a version that uses Tcl's variable traces
to set a global "bottle string" whenever the counter variable is set.
<lang tcl>proc setBottles {varName args} {
upvar #0 $varName n
set ::bottles [format "%d bottle%s" $n [expr {$n == 1 ? "" : "s"}]]
 
trace add variable i write setBottles
 
for {set i 99} {$i > 0} {} {
puts "$bottles of beer on the wall"
puts "$bottles of beer"
puts "take one down, pass it around"
incr i -1
puts "$bottles of beer on the wall\n"
}</lang>
 
 
===Wordy version===
Line 63 ⟶ 82:
incr i -1
line4 $i
}</lang>
 
===using variable traces===
Here's a version that uses Tcl's variable traces
to set a global "bottle string" whenever the counter variable is set.
<lang tcl>proc setBottles {varName args} {
upvar #0 $varName n
set ::bottles [format "%d bottle%s" $n [expr {$n == 1 ? "" : "s"}]]
 
trace add variable i write setBottles
 
for {set i 99} {$i > 0} {} {
puts "$bottles of beer on the wall"
puts "$bottles of beer"
puts "take one down, pass it around"
incr i -1
puts "$bottles of beer on the wall\n"
}</lang>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.