Exercises, examples and other material relating to training module T208. This topic is presented on public courses
Learning to program in Tcl,
Tcl Basics
Background
Arrays are variables that can contain a number of strings, and each one can be looked up by its index and key. Unlike arrays in other languages, array keys in Tcl are strings, not integers, and array members can contain strings of differing lengths. Dicts are an alternative to arrays that implement unordered, keyed collections through strings. You'll start this module learning about the basics of arraysi and dicts, and go on to learn some of the wider uses and extra power that's provided by their somewhat unusual specification, taught by example.
| Articles and tips on this subject | updated |
| 3582 | Tcl collections - lists, dicts and array In Tcl, almost all variables are what is described as "pure" - which means that they hold data as strings of text which can be passed, substituted with a $ prefix, etc. Special interpretation on pure variables allows them to be treated as:
• integers
• floats
• lists
• dicts
• ... | 2012-01-28 |
| 3415 | User defined sorting and other uses of callbacks in Tcl and Tk When I output a table of results, I usually want it to be sorted in some way.
In Tcl, I can use lsort to sort a list - there's an example of it running in a default way [here]. However, there's often a need to order records according to a non-default algorithm, and there are switches such as -integer ... | 2011-09-02 |
| 3192 | Tcl - Some example of HOW TO in handling data files and formats During the Tcl course I was running earlier this week, we got involved in a number of interesting topics such as
• How to clean up input lines into lists of fields (use split)
• How to reformat awkward fields (use regexp and regsub)
• How to combine files that vary in format from year ... | 2011-03-04 |
| 1282 | Stringing together Tcl scripts If you have a series of Tcl scripts that you want to run in a sequence, you can
call all of them up in a master script using a series of source commands. This command switches input from the current script (sources) to the new source given as if the text of the sourced file was copied in to the original ... | 2011-03-01 |
| 2466 | Tcl - passing arrays and strings in and back out of procs When you want to pass data INTO a proc in Tcl from a regular variable, you write the variable name with a $ in front of it in the code and the value is substituted. But this doesn't work if (a) you are passing an array (which cannot be expanded into a string or (b) you want to change the value in a ... | 2009-10-22 |
| 1614 | When an array is not an array An array, I was taught, is a sequential series of memory locations in which values of some type are stored. In an array, each of the memory locations is the same size (number of bytes). And so, as I was taught, arrays can be used very efficiently using pointer arithmetic, BUT they need to:
a) Be defined ... | 2008-04-19 |
| 1427 | Arrays in Tcl - a demonstration # Array in Tcl
set town(Wycombe) 120000
set town(Marlow) 32567
set town(Amersham) 17000
set town(Melksham) 24000
set town(1) xxxxxx
set town(1.) yyyyyyy
# Note that you CAN use numbers as subscripts but you
# must be careful because element "1" is different to
# element "1." ... or element ... | 2007-11-10 |
| 1405 | Sorting in Tcl - lists and arrays Tcl's lsort command lets you sort a list - and that can be a list of the keys of an array. You can't sort the array, but once you have the list of keys you can sort that and use it to iterate through the array in any order that you like.
As everything (except an array) is a string in Tcl, when you ... | 2007-10-25 |
| 1283 | Generating traffic for network testing "Why do you have a random number generator in a scientific language" ask some newcomers to the world of programming ... "shouldn't programming be an exact science?". Well yes, and no. It turns out that a random number generator is exceedingly useful in some instances. Here's a real, live example ... | 2007-07-29 |
| 779 | The fragility of pancakes - and better structures Have you ever hunted around a directory for a file ... you KNOW it's there, but you can't find it - can't see the wood for the trees, if you like, in a listing that spills over many pages?
I call such directory structures "pancakes" as they're thin and flat, and they tend not to be very easy to work ... | 2006-06-30 |
| 122 | Passing arrays to procs in Tcl If you're writing a Tcl proc, you'll normally pass in parameters by value, specifying a $ in front of the variable whos contents you want to pass in in the calling sequence:
dojob $passin
If, however, passin is a Tcl array, tis won't work since $passin cannot be expanded to string. So you'll ... | 2006-06-05 |
Examples from our training material
| ar1 | First use of arrays |
| ar2 | Checking if an array key exists |
| ar3 | looping though an array with array get |
| ar_rail | Setting up an array and sorting the elements by key |
| arr_log | Access log counter using arrays |
| arr_log_2 | Selecting elements from an array - glob matching to keys |
| arr_log_3 | Passing arrays in and out of procs - using upvar |
| dla | list, array, dict |
| ifinder | search for icon references; list icon names |
| ifinder2 | search for icon references and count them |
| stfinal | final exercise - array of dialling codes |
| topskill | setting up an array of lists, sorting, listing out elements |
Pictures
Arrays in Tcl
A small group learns Tcl on a private course
Background information
Some modules are
available for download as a sample of our material or under an
Open Training Notes License for free download from
http://www.training-notes.co.uk.
Topics covered in this module
First use of an array.
Setting up an array.
Referencing elements of an array.
Enquiring on an array.
First use of a dict.
Pure and impure variables.
Arrays rather than lists or dicts.
A wider range of array commands.
Array get and set.
Selecting elements from an array.
Uses of arrays.
Using numeric indexes.
Passing arrays to procs.
Unique keys.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our
Listing and schedule page.
Well House Consultants specialise in training courses in
Tcl/Tk, Expect,
Python,
Perl,
PHP, and
MySQL. We run
Private Courses throughout the UK (and beyond for longer courses), and
Public Courses at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if
you live in a different
country or continent to us.
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre. Also
available is the Opentalk
Forum for discussion of technical questions.