Tcl Powered!NeoWebScript

User Info
New User FAQ
Sysop Info
Sysop FAQ
Release Notes

Mini-Tutorial #3

A more in-depth look at Tcl

In this "mini-tutorial", we're going to take a closer look at Tcl in general. With all of the commands available to the NeoWebScript web developer, it is possible to create very powerful applications both easily and quickly. As with anything, the more you know about how commands and procedures work, the more you'll be able to do. NeoWebScript is based on a scripting language called Tcl, developed around 1990, that is very robust and capable when it comes to handling data. We'll be able to tap into that power and ability, but only if we know some of the basic rules Tcl uses in handling data. That's what this tutorial is about.

Where do my scripts go?

Before we begin, let's take a little look at how Tcl works in general, and see how our scripts are handled. Tcl is an interpreted language, rather than a compiled one. This means that there is a program that reads in your Tcl script and evaluates all of the commands. The commands we use in our Tcl scripts are not magical in any way--they are just important to the Tcl interpreter running on the computer. The interpreter takes our scripts and turns them into code that the computer can understand and execute. This is where NeoWebScript comes in. We took the Apache webserver, which is the most popular webserver in the world, and embedded a Tcl interpreter in it. What does this mean to you? Now you can put Tcl code directly inside of your web pages and have the webserver software evaluate it, using the built-in Tcl interpreter. This is where the power of NeoWebScript lies.

No need to get wound up...

Unlike some formal languages like "C" and "Java", Tcl only has one data type: the string. A data type describes a piece of information. For example, the term "integer" describes a whole number, while "decimal" describes a fractional number. The string data type describes a string of characters, in other words: letters, sentences, words and paragraphs. To repeat: data types describe a piece of information. They tell the computer what type of data it is. We use data types in our everyday lives whenever we ask a question. We check to see if the answer matches what we were expecting. Take the following converstation, for example:

Achilles:  "Hector, are you planning on going 
           to the Colosseum tonight?"
Hector:    "I sure am.  Could you pick up some 
           tickets for me on your way out?"
Achilles:  "No problem.  How many do you want?"
Hector:    "Blue"
Achilles:  "What?  I want to know how many 
           tickets you want!"
Hector:    "I enjoy walks around Troy."
Achilles:  "Arghhh!"

Obviously, Hector's answers were not what Achilles was expecting. Achilles wanted an integer (whole number), but Hector gave him a string. In much the same way Tcl commands expect a certain type of data, and if you give them the wrong type, they will get confused, and complain to you. Here's an example of the type of message NeoWebScript could give you, if you don't provide the command with what it was looking for:

Error: expected integer but got "asdf"	

Let's take a tour through the different ways that Tcl handles data.

Only one data type: the String

In earlier tutorials, we learned about how to use variables, and how to store information in them. All of this happens with the set command. Let's take a look at a simple example:

set CustomerName "Dionysus"
set productName "Wine"
set CustomerStatus "god"
set yearlyIncome "3932.92"
set litersToDate "827.00"

In the first line of this example, the variable "CustomerName" is set to the string "Dionysus". The following lines work the same way. It is important to realize that Tcl treats everything as a string. In this way, Tcl is very easy to use, since there is only one data type. There is no need to tell Tcl that a variable will hold a string, or an integer, or a decimal number. The example above points out that behavior. As mentioned in tutorial 1, NeoWebScript sets some variables for you to access in your scripts. Those variables are grouped together int he form of an array. We'll take a better look at arrays in the next section. This information can be accessed from the environment, via the webenv array.

Array your forces

NeoWebScript provides a lot of information about the current request. If you look back in tutorial 1, you'll see that everything from the time the file was last modified to the server the page resides on can be accessed and given to the user. This information is kept in a group of variables. This group of variables is called an array. An array is nothing more than a collection of variables that are all related. Arrays allow the NeoWebScript to organize and keep track of all the information concerning the current page. You too can organize and keep track of your information in the form of an array. Let's see how arrays are setup:

set my_info(Name) Thor
set my_info(Tool) Hammer
set my_info(Job) "Counting stones in Wales"

In the above example, the array is called "my_info". This name can really be anything, as long as there are no spaces or special characters. See tutorial 1 for more information on variable names. Name, Tool, and Job are all members of the array "my_info". Calling arrays is as simple as calling variables--you simply use the "$" symbol. For example:

set my_info(Name) Thor
set my_info(Tool) Hammer

html "Hello, $my_info(Name).<br>"
html "You use the $my_info(Tool)."


Hello, Thor.
You use the Hammer.

List your demands

There is a special type of string in Tcl called a list. A list is a collection of "things", including text, strings, integers, etc. These things are joined together and act as one unit, which can be put into a variable, sent to a command, or passed around. Although the list acts as one unit, you can access each individual member. The list is used most often when you have a group of similar things. For example, I could create a list of people going to my birthday party, and still have access to each name. It their simplest form, lists are just strings with spaces separating each element:

Jessica "David P" Mindy "David L"

To group together a list of items, you can use the list command. The list command takes any number of items and returns a list generated by them. In the above example, I used the "" (double-quotes) to signify that some of the elements had spaces in them. Let's see the list command in action:

set myFriends [list Jessica "David P" Mindy "David L"]
html $myFriends

Jessica {David P} Mindy {David L}

Notice how elements that have spaces in them are grouped together using the curly braces. You can also create a list that contains sub-lists! Take a look:

set myFriends [list Jessica {Monica {Louis Ted}} Charles]
html $myFriends

Jessica {Monica {Louis Ted}} Charles

Commands that Handle Lists

What are some of the things you can do with a list? Here are some of the commands that act on lists, with descriptions of what they do listed as well:

  • lsort: Sorts a list alphabetically or numerically
  • llength: Returns the number of elements in a list
  • lindex: Returns a particular element of a list
  • lappend: Append an element to a list
Let's see an example of each of these commands in action:


set the_colors {Red Blue Yellow Indigo Violet }
html "Unsorted:  $the_colors"
html "Sorted:  [lsort $the_colors]"

Unsorted: Red Blue Yellow Indigo Violet
Sorted: Blue Indigo Red Violet Yellow

In the above example, the list "the_colors" was unsorted to begin with. The lsort command sorted them into a more orderly list. Since we surrounded the command with the square brackets [...], the Tcl interpreter took the results of that command and replaced the command with the result. It was just like we typed in the the sorted list directly into the html command.


set the_colors {Red Blue Yellow Indigo Violet }
html "The number of elements is:"
html "[llength $the_colors]

The number of elements is: 5


set the_colors {Red Blue Yellow Indigo Violet }
set my_color [lindex $the_colors 3]
html $my_color


The lindex command pulls out a particular element from the list. Notice that although I specified the third element "3", I got the fourth element from the left. This is because Tcl starts counting from Zero. So in the list given above, Red is the "Zeroith" element of the list, Blue is the "First", Yellow is the "Second", and so forth. Many commands act like this in Tcl and other languages.

The lappend commands adds elements to a list. Let's see it below:

set some_colors {Red Green Blue}
lappend some_colors {Orange}
lappend some_colors {Gray White Black}
html $some_colors

Red Green Blue Orange {Gray White Black}

Beyond the basics

There are some other commands that handle lists, and some particulars that allow you to further manipulate data, but for most simple NeoWebScript work, these are enough. Knowing a little about what commands expect and how to get your information into the form they need is important, and with arrays and lists, you can do that. For example, the procedures that handle storing information into a database expect you to give them an array of data to store, while commands like random_pick_html expect a list of data.

For more information on the particulars of certain commands, take a look at the main NeoWebScript site, at

Copyright © 1995-1999 NeoSoft Inc., 1770 St. James Place, Suite 500, Houston, TX 77056 USA. All Rights Reserved