99 Bottles of Beer/Tcl: Difference between revisions

m
Fixed syntax highlighting.
(moved code from main task-page to sub-page)
m (Fixed syntax highlighting.)
 
(One intermediate revision by one other user not shown)
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.
<langsyntaxhighlight 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"
}</syntaxhighlight>
 
 
===Wordy version===
<langsyntaxhighlight lang="tcl">set s "s"; set ob "of beer"; set otw "on the wall"; set more "Take one down and pass it around"
for {set n 100} {$n ne "No more"} {} {
switch -- [incr n -1] {
Line 11 ⟶ 30:
lappend verse "\n$n bottle$s $ob $otw, [string tolower $n] bottle$s $ob.\n$more"
}
puts -nonewline [join [lreplace $verse 0 0] ""][lindex $verse 0]</langsyntaxhighlight>
 
Version which converts numbers to words, optimized for script length while retaining readability:
<langsyntaxhighlight lang="tcl">proc 0-19 {n} {
lindex {"no more" one two three four five six seven eight nine ten eleven
twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen} $n
Line 40 ⟶ 60:
 
puts "No more bottles of beer on the wall, no more bottles of beer."
puts "Go to the store and buy some more, 99 bottles of beer on the wall."</langsyntaxhighlight>
 
 
===99-bottles-of-beer.net===
from http://99-bottles-of-beer.net/language-tcl-439.html
<langsyntaxhighlight lang="tcl">proc bottles {i} {
return "$i bottle[expr {$i!=1?{s}:{}}] of beer"
}
Line 63 ⟶ 83:
incr i -1
line4 $i
}</langsyntaxhighlight>
 
===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>
 
===The Boozy Version===
Line 92 ⟶ 94:
and not require any extensions.<br>
{{works with|Tcl|8.4}}
<langsyntaxhighlight lang="tcl"># 99 bottles of beer on the wall, Expect-style
# Author: Don Libes <libes@nist.gov>
### Adapted by: Donal K. Fellows <donal.k.fellows@manchester.ac.uk>
Line 229 ⟶ 231:
set i [expr {6+int(3*rand())}]
}
}</langsyntaxhighlight>
9,486

edits