Version 9 of Tcl cheat sheet

Updated 2004-01-18 01:16:37

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


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 here. See set


PHP: $var always means one thing

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

TCL: $var and 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: "numeric" arrays and "associative" arrays

PERL: "arrays" and "hashes"

TCL: "lists" and "arrays"


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...

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.


Category ???