Well House Consultants
You are on the site of
Well House Consultants
Open Source Training Courses
hotel accommodation. You are welcome to browse and use
our resources subject to our copyright statement and to add in links from your pages to ours.
Other subject areas - resources
Well House Manor Resources
Object Orientation and General topics
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
C and C++ Resources
Web and Intranet Resources
Tcl/Tk module T208
Arrays and dicts
Exercises, examples and other material relating to training module T208. This topic is presented on public courses Learning to program in Tcl
, Tcl Programming
, Tcl Programming
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|
|3638||Sorting dicts and arrays in Tcl|
In Tcl, both arrays and dicts can contain key / value pairs, where the keys are any unique string. What are the differences?
arrays are NOT simply formatted strings - they're a special structure which uses hashing techniques for managing the members, making it very fast for Tcl to access members by ...
|3614||Tcl - dicts - a tutorial and examples|
At Tcl 8.5, the dict command was added to Tcl.
What is a dictionary
A dict used the same formatting as a list - in other words, it's a string of text (just like every other variable type except an array) which is handled through the same tokeniser / interpreter that's used for the source ...
|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:
|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 ...
|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 ...
|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 ...
|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 ...
|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 ...
|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 ...
|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 ...
|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 ...
|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 ...
|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:
If, however, passin is a Tcl array, tis won't work since $passin cannot be expanded to string. So you'll ...
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|
|au|| Demo of use of upvar|
|dicdem|| dicts - using the various subcommands|
|dict1|| setting up and get, dict|
|dict2|| Conrol - using a string as a list|
|dict3|| iterating through the keys of a dict|
|dict4|| dict for|
|dla|| list, array, dict|
|dnicer|| test program for station data|
|ifinder|| search for icon references; list icon names|
|ifinder2|| search for icon references and count them|
|mini|| 2nd program using railutils|
|nested|| nested dicts|
|rail_dict|| Sorting a dict|
|railutils|| File of procs to do with station info|
|stfinal|| final exercise - array of dialling codes|
|topskill|| setting up an array of lists, sorting, listing out elements|
PicturesArrays in Tcl
A small group learns Tcl on a private course
Some modules are available for download
as a sample of our material or under an Open Training Notes License
for free download from [here]
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.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
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.