02 Basics - Lesson 1

Folks,

here comes, finally, the long awaited lesson #1 :-)

To silence the voices in me trying to drive me to perfectionism,
I have decided to declare this class to be in alpha testing stage.
Therefore, I'd like to ask you for feedback, comments, and corrections
of both the facts and my English, if you feel that something just
sounds strange.

It will also helpful to know what parts of the text were (too) hard to
understand, too short, too long - I think you get the picture :)

The lessons start with a truly basic question: How do we get
our data into the script?

Have fun!

Sonja

************************************************************************

Lesson #1: Data (I)

The easiest way to get data into a program is to write it directly into
the code. It is then called "literal", and the rules how to write this
down are language dependent. It is quite common to have some textual and
numerical data written right into the code of a program. (This is
sometimes called "hardcoded into the program/script".)

Textual data: Strings (as used in Tcl)

Unless told otherwise, tcl treats everything as string. As commands and
arguments are usually separated by spaces, any string containing spaces
must be enclosed within single or double quotes. (For clarity, I use
quotes around single-word strings as well, and I'd advise you to do the
same.)

If you leave out the quotes, tcl will treat every word of your
string data as single argument and probably complain about the wrong
number of arguments, or do strange and unintended things. The 'puts'
command we already used in our "Hello World" example in lesson #0 accepts
a string as single argument, so I will use it for some basic examples:

puts string_data
puts "String data"

The following will not work, but result in an error from the interpreter:

puts String data

(Tcl is actually not complaining about the wrong number of arguments, but
about not being able to 'find a channel named "String"'. Please be patient,
we'll come back to that later.)

There are characters, even in the ASCII character set, that can't be
input with the keyboard or printed on the screen. Other characters can,
but have some meaning in the syntax of Tcl itself and need special
treatment if used literally in a string. These characters are written
as a combination of two or more letters starting with a backslash ("\").
Examples are:

\a Terminal bell (your computer beeping at you :)).
\n Newline

(The newline character is used in Tcl to separate commands, so if you need
one in a string, you have to encode it.)

The backslash is also used to take away special meaning from other
characters:

\" a quote (would terminate the string if used without the "\")
\ the backslash itself

Examples:

puts "It is possible to use the \" character in a string by typing \\""
puts "This will be invisible, but audible: \a"

Full coverage of backslash substitution in Tcl can be found on

http://dev.scriptics.com/man/tcl8.3/TclCmd/Tcl.htm

or with the command

$ man Tcl

under [8]. You don't have to memorize everything :) - but if anything is
truly and completely Chinese to you, please ask for a more detailed
explanation on the list. [1] - [7] and [9] - [11] are an interesting (and
recommended) read for the about Tcl.

How strings and backslash substitution are handled may differ in detail in
different programming languages, but the main concept stays the same. The
backslash combinations are often called "escape sequences", and the
backslash the "escape character".

Numerical data: Numbers

There are two major types of numbers: integers and floating point numbers.
Integers (whole numbers) can be written in decimal, octal and hexadecimal
notation:

Decimal: 3, 5, 123, 28736487
Octal: 01, 07, 010, 023 (the leading zero marks these as octal values)
Hexadecimal: 0x1, 0xff, 0xDEADBEEF (leading 0x means "hexadecimal")

Floating point numbers are always written in simple decimal notation or
scientific notation:

Simple: 1.2345, 1.0, 3.1416
Scientific: 1.2345e6 (meaning "1.2345 times 10 to the sixth (power)",
i.e. 1234500)

(If octal, hexadecimal and scientific notation are strange concepts to you,
don't worry. I'll explain in detail when you really need to know how to handle
these. For the time being, it's sufficient to know that integers are whole
numbers, and floating point numbers are numbers written with a decimal point,
even if this is a rather gross oversimplification :))

Again, how numbers are written down in source code is roughly the same across
programming languages, so the abave is not only valid for Tcl.


Doing simple math in Tcl

I'd like to encourage you to play around with numbers in Tcl a little bit to
get a feeling how to do basic calculations. For this, you need the Tcl
command 'expr'. It assumes all arguments given to be a mathematical expression
and executes that. To output the result of the calculation to the screen, you
enclose the 'expr' command and all its arguments in square brackets ("[" and
"]") and use the whole expression as an argument to 'puts':

puts [expr 1 + 1]
puts [expr 1 - (2 * 3)]
puts [expr (1 - 2) * 3]
puts [expr 07 + 1]
puts [expr 1.2e3 * 4]
puts [expr 0xff / 16]

(addition: +, substraction: -, multiplication: *, division: /)

You find more information about 'expr' on:

http://dev.scriptics.com/man/tcl8.3/TclCmd/expr.htm

Exercises:

Remember how to make an executable Tcl script from lesson #0? Create a file
that has

#!/usr/bin/tclsh

as its very first line (use the actual path to tclsh on your system, of
course), add your code, save and close the file, and make it executable with

$ chmod a+x <filename>

Run it with

$ ./<filename>

in the directory where you saved it.

Try out every example I gave in this lesson by typing it into a tcl script
file created in this manner and running the script(s). Experiment, read the
documentation I mentioned, and play around with what you know until you're
bored.

If you have questions, please do ask them on the list, and don't be afraid
to sound stupid or silly. This is for beginners, remember? :-)

******************************************************************************