Version 18 of Tcl cheat sheet

Updated 2004-01-19 14:24:00

New in Tcl? Coming from another language? Read on.

This could be fun. I learned almost all of my programming with PHP, and a little bit of Perl. This sheet [L1 ] helped me an awful lot when I tried to learn Perl, so a Tcl cheat sheet may be awfully useful to someone else. Feel free to add other languages.

How about we get the Tcl stuff correct before adding other languages? Roy Terry, 17Jan2003


PHP: file.php3

PERL: file.cgi

TCL: file.tcl


PHP: scripts in <?php..?>

PERL: whole file is script

TCL: whole file is script (unless you use mod_dtcl in Apache 1.3.x or mod_tcl/rivet in Apache 2.x, there everything is like PHP)


PHP: can include raw HTML

PERL: must print all output

TCL: must print all output (unless you use mod_dtcl in Apache 1.3.x or mod_tcl/rivet in Apache 2.x, there everything is like PHP)


PHP: no requirements for first line

PERL: first line is #!/usr/local/bin/perl or any other path to your Perl interpreter (but is that a requirement?)

TCL: first line can be #!/usr/local/bin/tclsh or any other path to your Tcl interpreter (but that is absolutely irrelevant if you are using Windows). See exec magic for the recommended way to start a Tcl file if you want it to behave like an app on *nix systems.


protections

PHP: not executable file

PERL: executable file

TCL: What the heck? You can make almost any file executable in nix platforms!


PHP: chmod 644 file.php3

PERL: chmod a+x file.cgi

TCL: chmod a+x file.tcl


printing

PHP: echo "stuff\n";

PERL: print "stuff\n";

TCL: puts stuff OR puts "some stuff" OR puts {some stuff}. It may be useful here to point out that Tcl adds a trailing newline by default. To disable this behaviour use the -nonewline option to puts.


variables

PHP: All variables $var

PERL: $var is a scalar variable.

TCL: var refers to the name of a variable which is needed every time you want to modify its contents (write to the variable); or $var referes to the content of the variable to be used when you want to read the variable . See set


PHP: $var always means one thing

PERL: $var, @var, %var, var are different things!

TCL: since all variables have a $var in front, $var always means the same thing. But beware: var could be scalar or an array variable and has to be used according to that!


arrays

PHP: $var = array(1,2,3);

PERL: @var = (1,2,3);

TCL: well, there are lists which work very fine, plus there are arrays (which are called hashes in some other languages), see below

 set var "1 2 3"  '''OR'''  set var [ list 1 2 3 ] while the latter is preferred especially in cases where the contents are not as simple as "1 2 3", but may contain spaces, return values from functions and so on. Only the list command helps you to correctly use quoting.

and arrays...

 array set var "key1 value1 key2 value2 key3 value3"

PHP: "numeric" arrays and "associative" arrays

PERL: "arrays" and "hashes"

TCL: "lists" and associative "arrays" - of course the associative arrays can have numbers as indexes.

(NEM and we now have dicts)


PHP: $var[1] is second element of $var

PERL: $var[1] is second element of @var

TCL: [ lindex $var 1 ] is second element of $var


PHP: count($var) is length

PERL: scalar(@var) is length

TCL: [ llength $var ] is length of list, and [llength [array names var] is number of elements in array.


PHP: for ($i=0; $i<count($var); $i++) echo $var[$i];

PERL: foreach $thing (@var) { print $thing; }

TCL: for { set i 0 } {$i < [ llength $var ] } { incr i } { puts [ lindex $var $i ] }

NEM This Tcl example would be far better written as: foreach thing $var { puts $thing }

Working through an array works like this:

 foreach index [array names var] {
    puts $var($index)
 } ;# foreach

PHP: join(":",array(1,2,3))

PERL: join(":",(1,2,3))

TCL: join [ list 1 2 3 ] : (yeah, yeah, you can wrap the colon in quotes if you're queasy)


objects

PHP: associative arrays are arrays

PERL: associative arrays are their own type

TCL: Er... what is a "type"? ;-) If you like objects you could use incr Tcl or stooop


PHP: $var = array(1=>"ho","hi"=>4);

PERL: %var = (1=>"ho","hi"=>4);

TCL: array set var "1 ho hi 4"


Access value of single array element 'ho'

PHP: $var['ho']

PERL: $var{'ho'}

TCL: $var(ho) (for constants with no white space quotes not needed)


PHP: while (list($k,$v) = each $var)) {...}

PERL: while (($k,$v) = each %var) {...}

TCL: er... foreach { key value } [ array get var ] {...}


to be continued...

Roy Terry, 17Jan2003 - say this looks helpful. On your next pass can you be more specific on 1) What each section is trying to demonstrate, and 2) your questions about Tcl. "??? - confusing" doesn't give folks a good idea of what additional info you need. One obvious correction: Tcl uses parens, not square brackets, to index arrays. Further more the single quote has absolutely no special meaning in Tcl, it does not quote things and it will not be removed by the parser; so best to avoid it. Now I see something else: In Tcl variables can be either an array or a string value and that's determined by usage.

LES: It's not Tcl that confuses me. It's the PHP and Perl examples that confused me, so I am not sure how to translate them into Tcl. For example: instead of saying $var['ho'] is element indexed by 'ho' , I'd rather say "ho" is element indexed by $var['1']. Do I make myself clear? RT - no I don't think you do. Take a look at the array access segment now. I think it helps to put the "operation" at the top of the section and then just list the code sample.


Category Languge