Exercises, examples and other material relating to training module T205. This topic is presented on public courses Learning to program in Tcl
, Tcl Programming
, Tcl Programming
The Tcl language is built around strings Ð everything is a string! In this module we look at basic string handling commands, the string command itself which provides a flexible selection of facilities, and we introduce regular expressions for pattern matching.
Related technical and longer articlesAnalysing incoming data lines
|Articles and tips on this subject||updated|
|4455||Working out distance between places, using OS grid references and a program in Tcl|
In Great Britain, locations have Ordnance Survey grid reference positions which place them to within 100 metres. Our training centre / hotel is at ST907633 - that's
100 km square ST
90.7 km into that square from the west side
63.3 km into that squate from the south ...
|4454||Everything is a string - even a list|
Almost all variables in Tcl are held as strings ... if you use integers and stirings in calculations such as expr and incr. the strings are converted back and forth intergally. Lists, too, are held as strings of characters in which chunks of string are counted / taken out as being elements in the list. ...
|4205||Regular Expression Substitution - Tcl|
regexp matches regular expressions in TCL, and it will return the latest match into a series of variables if you specify their names. regsub matches and replaces... not quite such a common requirement, and perhaps something I haven't blogged about in the past.
set newstory [regsub blue $original ...
|3576||Tcl - apparently odd behaviour of string trimleft|
Why does this code:
set dir "D:/JNN/proc/"
set tempname "D:/JNN/proc/Doc-99887767.dat"
set cname [string trimleft $tempname $dir]
but this code:
set dir "D:/JNN/proc/"
|3285||Extracting data from a string / line from file - Tcl|
I never cease to be amazed at the number of different ways that data can be encoded into simple test lines ... and how the various languages that we teach can be used to manipulate / extract pertinent information. Today, on a Tcl Course, I was presented with data in this form:
|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 ...
|404||How to check that a string contains a number in Tcl|
In Tcl, all variables hold strings. If you perform an arithmetic operation such as an expr, an incr or a numeric comparision, the incoming strings are converted into a numbers internally, the calculations are done, and the results are converted back to strings and saved. It might not sound very efficient, ...
|2472||split and join in tcl and expect|
Split and join in most languages convert strings of text into arrays / lists / collections of other sorts. But in Tcl, all variables are held as strings, so are split and join actually needed?
If you're working with a collection of single words - no embedded spaces, no special characters, space delimited, ...
|1601||Replacing the last comma with an and|
If I have a list, I'm likely to want to present it comma separated for the most part, but with the word "and" between the last two elements.
"Cambridge, the M11, the M25, the M4, the A361 and Melksham" for example.
Lists or arrays can be joined in almost any language with a function called join or ...
|1410||Tcl / regsub - changing a string and using interesting bits|
Regexp matches a string to a regular expression, and regsub goes one further in that it replaces the found string with something else, saving the transformed output into a new variable. But what if I want the output pattern to include part of the string that was matched? I can refer to the "interesting ...
|1403||Square Bracket protection in Tcl|
I was writing some string match examples in Tcl yesterday - and had the need to explicitly match square brackets within my string. But I couldn't just write the square brackets into the match string ...
• The square bracket first needed protection from the Tcl parser before it even got to the ...
|943||Matching within multiline strings, and ignoring case in regular expressions|
Regular Expressions are powerful matching tools and you can specify almost anything within them. But there are certain facilities that are naturally applied to the regular expression as a whole rather than to parts of the match, and there are specified in a different way in each language / implementation.
|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 ...
Examples from our training material
|ac_report|| Complete example - Access log file analysis|
|apfo|| format and append commands|
|brax|| Literally matching round brackets in regular expressions|
|ccat|| lappend and concat - the difference|
|datematch|| Matching a date using a regular expression|
|dm2|| A further date match example|
|hunter|| Example of analysis of a log file|
|iconfinder|| search for all icon references in a web log|
|match|| String matching - using globing|
|poco|| Postcode identifier and string splitter|
|rematch|| String matching using a regular expression|
|rf|| Opening and reading a file|
|rf2|| Using scan to get a field from a string|
|rf2a|| Using scan's return value|
|stringinfo|| Getting information from a text string|
|stringman|| Manipulating a string|
PicturesString matching in Tcl
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
Basic string handling commands.
The string command.
The elements of a regular expression.
Manipulating matched regular expressions.
Putting it together.
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.