Version 2 of Tcl cheat sheet

Updated 2004-01-17 23:15:05

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.


PHP: file.php3

PERL: file.cgi

TCL: file.tcl


PHP: scripts in <?php..?>

PERL: whole file is script

TCL: whole file is script


PHP: can include raw HTML

PERL: must print all output

TCL: must print all output


PHP: no requirements for first line

PERL: first line is #!/local/bin/perl (but is that a requirement?)

TCL: first line can be #!/local/bin/tclsh (but that is no requirement)


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 755 file.cgi

TCL: er... I don't know


printing

PHP: echo "stuff\n";

PERL: print "stuff\n";

TCL: puts stuff OR puts "some stuff" OR puts {some stuff}


variables

PHP: All variables $var

PERL: $var is a scalar variable.

TCL: var or $var - more detailed explanation needed


PHP: $var always means one thing

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

TCL: $var always means one thing


arrays

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

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

TCL: well, there are lists...

 set var "1 2 3"  '''OR'''  set var [[ list 1 2 3 ]]

and arrays...

 array set var "key1 value1 key2 value2 key3 value3"

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


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 ] }


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"? ;-)


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

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

TCL: array set var "1 ho hi 4"


PHP: $var['ho'] is element indexed by 'ho'

PERL: $var{'ho'} is element of %var

TCL: $var['ho'] is element indexed by 'ho' confusing???


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

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

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


to be continued...


Category ???