if 0 { <> ---- **Introduction** [MiHa] 2015-06-13: HelloWorld-programs are supposed to be the most simple, working programs of a given language. But I want to extend such a HelloWorld-program to a one-page - reference-card, <
> where all the essential features of the language are shown. <
> Several short programs would be ok, too. <
> If possible, while doing something useful (and/or funny). Tcl by itself (without Tk) is http://wiki.tcl.tk/10259%|%quite simple%|%, so an amount of code to fill about one printed page should be enough. <
> (The layout may need to be tweaked, e.g. printing front and back, landscape, and in 2-4 columns:) Basic [Tcl Commands] to cover: * http://wiki.tcl.tk/1669%|%Comments%|% / statements * Output: [puts] / -nonewline ** [format] * Numeric [variable]s: [set] * multiple statements / assignments in one line ** simple calculations: [incr] / [expr] * [proc] / [return] - for functions * basic flowcontrol: ** [if] / then / else / elseif / comparing ** [for] - loop, maybe also the [while]-loop / [break] / [continue] * [foreach] * String variables / quoting * [exit] Advanced features (to include if space permits): * input: [gets], [eof], [scan] / checking the input <
>This is an important topic, but likely too much for one page. * [catch] * [global] / $::var * [string]-operations (e.g. find/extract/replace char in a string, append, delete/split ...) * [data structure]s: ** [array] / [parray] ** [list] / [lassign], [lappend], [lset], [linsert], [lreplace] / [lindex], [llength] / [lrange], [lsearch], [lsort] ** [dict] * [file]-operations / [open], [close ] / [glob] ... More features, to be left out, for a later lesson (too advanced, or too rarely needed for an introduction): * [upvar] / [uplevel] * [namespace]s * [eval] * [interp] * [lambda] * [regexp] - this is very likely too complex to cover as just one small example on a general introduction page. * [switch] * [unknown] ... Things to be aware of: * comments are commands, so quotes and braces inside need to be balanced * when to use / not to use "$" with variablenames ( set i 1; puts $i; incr i $i ) Maybe there already are some programs here in the wiki that would qualify for such a refcard-program... } ---- **Program 1 - puts** ======tcl # HelloWorld001.tcl - 2015-06-13 - http://wiki.tcl.tk/41268 # Output a string to terminal: puts "Hello, World!" ### END ### ====== ---- **Program 2 - set, incr** ======tcl # HelloWorld002 - 2015-06-23 # http://ideone.com/9KlN4c # Assign value to variables, and output: set a "The answer is" set n 42 puts $a puts $n incr n ;# add 1 to a numeric variable puts "$a now $n" # END # ====== ***Output 2*** ======none The answer is 42 The answer is now 43 ====== ---- **Program 3 - clock, timedate, if, else** ======tcl # HelloWorld003.tcl - MiHa - 2015-06-13 # http://wiki.tcl.tk/41268 / http://ideone.com/xl0IBI # Query the time (cs as a number, td and hh as a string), # output depending on the value of the hour: set cs [clock seconds] ;# seconds since 1970-01-01 set td [clock format $cs -format "%Y-%m-%d %H:%M:%S" ] ;# timedate set hh [clock format $cs -format %H ] ;# hours (00-23) puts "cs=$cs td='$td' hh=$hh" if { $hh < "12" } { puts -nonewline "Good morning, " } else { puts -nonewline "Hello, " } puts "World!" # END # ====== ***Output 3*** ======none cs=1434926575 td='2015-06-21 22:42:55' hh=22 Hello, World! ====== ---- **Program 4 - clock, if,elseif** ======tcl # HelloWorld004.tcl - MiHa - 2015-06-21 # http://wiki.tcl.tk/41268 / http://ideone.com/Llp8Os # Query the hour from the clock, # output text depending on the value of the hour: set cs [clock seconds] ;# seconds since 1970-01-01 set hh [clock format $cs -format %H ] ;# hours (00-23) puts "cs=$cs hh=$hh" if { $hh < "12" } { puts -nonewline "Good morning, " } elseif { $hh > "18" } { puts -nonewline "Good evening, " } else { puts -nonewline "Hello, " } puts "World!" # END # ====== ***Output 4*** ======none cs=973531773 hh=17 Hello, World! ====== and ======none cs=1434927642 hh=23 Good evening, World! ====== ---- **Program 5 - string compare** ======tcl # HelloWorld005 - 2015-06-23 # http://ideone.com/OJoort # Comparing strings: set hh "20" set tx "Hello" if { $hh < "12" } { set tx "Good Morning" } if { $hh > "18" } { set tx "Good Evening" } puts "It is $hh:00 --> $tx, World !" #. ====== ***Output 5*** ======none It is 20:00 --> Good Evening, World ! ====== ---- **Program 6 - proc** ======tcl # HelloWorld006 - 2015-06-23 # http://ideone.com/GBiMab # Comparing strings, proc as function proc greeting {hh} { set gr "Hello" if { $hh < "12" } { set gr "Good Morning" } if { $hh == "12" } { set gr "Happy Lunchtime" } if { $hh > "18" } { set gr "Good Evening" } return $gr } set hh "12" set tx [greeting $hh] puts "It is $hh:00 --> $tx, World !" #. ====== ***Output 6*** ======none It is 12:00 --> Happy Lunchtime, World ! ====== ---- **Program 7 - while, gets** ======tcl # HelloWorld007 - 2015-06-23 # http://ideone.com/mcV6YL # Comparing strings, proc as function, input in while-loop proc greeting {hh} { # This proc returns a result as soon as a match is found: set gr "Hello" if { $hh < "12" } { return "Good Morning" } if { $hh == "12" } { return "Happy Lunchtime" } if { $hh > "24" } { return "Goodbye" } if { $hh > "18" } { return "Good Evening" } return $gr } set hour "99" ;# start with any value that keeps the loop going while { $hour > "0" } { puts -nonewline "Enter hour, 0 to quit: " set hour [gets stdin] puts $hour puts "It is $hour:00 --> [greeting $hour], World !" } puts "Bye!" #. ====== ***Output 7*** ======none Enter hour, 0 to quit: 1 It is 1:00 --> Good Morning, World ! Enter hour, 0 to quit: 11 It is 11:00 --> Good Morning, World ! Enter hour, 0 to quit: 12 It is 12:00 --> Happy Lunchtime, World ! Enter hour, 0 to quit: 15 It is 15:00 --> Hello, World ! Enter hour, 0 to quit: 17 It is 17:00 --> Hello, World ! Enter hour, 0 to quit: 18 It is 18:00 --> Hello, World ! Enter hour, 0 to quit: 19 It is 19:00 --> Good Evening, World ! Enter hour, 0 to quit: 25 It is 25:00 --> Goodbye, World ! Enter hour, 0 to quit: 0 It is 0:00 --> Good Morning, World ! Bye! ====== ---- **Program 8 - for, format** ======tcl # HelloWorld008 - 2015-06-23 # http://ideone.com/lKo08L # Comparing strings, proc as function, for-loop, format proc greeting {hh} { # This proc returns a result as soon as a match is found: set gr "Hello" if { $hh < "12" } { return "Good Morning" } if { $hh == "12" } { return "Happy Lunchtime" } if { $hh > "24" } { return "Goodbye" } if { $hh > "18" } { return "Good Evening" } return $gr } puts "Greeting:" for {set h 0} {$h<=25} {incr h} { #set hour $h ;# Tcl converts number to string, as needed #set hour [format "%2d" $h] ;# format as numeric string with 2 digits set hour [format "%02d" $h] ;# format as 2 digits with leading 0 puts "at $hour:00 --> [greeting $hour], World !" } puts "Done." #. ====== ***Output 8*** ======none Greeting: at 00:00 --> Good Morning, World ! at 01:00 --> Good Morning, World ! at 02:00 --> Good Morning, World ! at 03:00 --> Good Morning, World ! at 04:00 --> Good Morning, World ! at 05:00 --> Good Morning, World ! at 06:00 --> Good Morning, World ! at 07:00 --> Good Morning, World ! at 08:00 --> Good Morning, World ! at 09:00 --> Good Morning, World ! at 10:00 --> Good Morning, World ! at 11:00 --> Good Morning, World ! at 12:00 --> Happy Lunchtime, World ! at 13:00 --> Hello, World ! at 14:00 --> Hello, World ! at 15:00 --> Hello, World ! at 16:00 --> Hello, World ! at 17:00 --> Hello, World ! at 18:00 --> Hello, World ! at 19:00 --> Good Evening, World ! at 20:00 --> Good Evening, World ! at 21:00 --> Good Evening, World ! at 22:00 --> Good Evening, World ! at 23:00 --> Good Evening, World ! at 24:00 --> Good Evening, World ! at 25:00 --> Goodbye, World ! Done. ====== ---- **Program 9 - expr, format** ======tcl # HelloWorld00x.tcl - 2015-06-xx #. ====== ***Output 9*** ======none xx ====== ---- Some ideas: * Table of primes * Dice-rolling ** with min/max/average * Calculate distance * Number of days between dates ... ---- **Program x** ======tcl # HelloWorld00x.tcl - 2015-06-xx #. ====== ***Output x*** ======none xx ====== ---- ---- **Remarks** This is the Alpha-version, there ''will'' be bugs, so use with caution <
> ... ---- '''See also:''' * http://wiki.tcl.tk/41210%|%ClockDemo%|% * ... <> Example | Tutorial