In Tcl commands, parameters are separated by spaces, just like Tcl
lists are. So it would sometimes be very useful to be able to write
set action {piemiddle apple}
set $action
to take a list of parameters from a Tcl list - in this case setting the "piemiddle" variable to the value "apple".
But it doesn't work. The problem is that the variable $action is only expanded into the string "piemiddle apple"
after the command line has been split into its individual parameters, so that the Tcl program attempts to refer to a single variable called "piemiddle apple" with a space in the middle of it, giving the message:
can't read "piemiddle apple": no such variable
which is a huge pity.
However, as of Tcl version 8.5 the
expand operator was introduced, which allows you to specify that a variable is to be expanded
before the command line is split into individual parameters. So if you write
set action {piemiddle apple}
set {*}$action
you will actually end up setting a variable called "piemiddle" to the value "apple".
The sequence
{*} is, then, know as the expand operator, and forms a part of a parameter that's to be expanded early. In other words, you may not add a space between the {*} and the name of the variable to be expanded. Full source code example
[here].
Note - in earlier versions of Tcl, the eval command was the recommended alternative and it remains available today.
eval is often very messy to implement, as it applies to the whole command rather than just an individual scalar list, and so you end up having to add all sorts of extra protections. Here's a simple
eval example - equivalent of the examples above:
set pudding {rolypolymiddle jam}
eval set $pudding
puts "In rolypoly we have $rolypolymiddle" (written 2012-01-17, updated 2012-01-28)
Associated topics are indexed under
T214 - Tcl/Tk - Other Facilities in Tcl [3570] Trapping errors in Tcl - the safety net that catch provides - (2012-01-06)
[3287] Exceptions - Tcl style - (2011-05-12)
[2467] Tcl - catching an error before your program crashes - (2009-10-22)
[1338] Handling Binary data in Tcl (with a note on C) - (2007-09-09)
[1334] Stable sorting - Tcl, Perl and others - (2007-09-06)
[1277] AgtInvoke - a command to drive Agilent Tcl software extensions - (2007-07-26)
[782] Converting between Hex and Decimal in Tcl - (2006-06-28)
[748] Getting rid of variables after you have finished with them - (2006-06-06)
[461] Shortened interactive commands - (2005-10-11)
[366] Error handling in Tcl through catch - (2005-07-02)
[364] pu daily and p hourly - (2005-06-30)
[239] What and why for the epoch - (2005-03-08)
T206 - Tcl/Tk - Lists [3618] lists and struct::list in Tcl - Introduction to struct::list and examples - (2012-02-18)
[3582] Tcl collections - lists, dicts and array - (2012-01-16)
[3415] User defined sorting and other uses of callbacks in Tcl and Tk - (2011-09-02)
[3394] The difference between lists and strings - Tcl - (2011-08-16)
[3285] Extracting data from a string / line from file - Tcl - (2011-05-10)
[2472] split and join in tcl and expect - (2009-10-23)
[2468] What are Tcl lists? - (2009-10-22)
[1601] Replacing the last comma with an and - (2008-04-04)
[1405] Sorting in Tcl - lists and arrays - (2007-10-24)
[1402] Tcl - append v lappend v concat - (2007-10-23)
[1283] Generating traffic for network testing - (2007-07-29)
[1282] Stringing together Tcl scripts - (2007-07-29)
[781] Tcl - lappend v concat - (2006-06-27)
[463] Splitting the difference - (2005-10-13)
[144] Tcl sandwich - lists in Tcl - (2004-12-08)
Some other Articles
C++ Courses - do I get official certification at the end of my Well House course?Emergency First Aid Provision at Well House Manor - part of the serviceThe changing Hotel and Bed and Breakfast business in MelkshamQR codes - graphics images that provide quick phone linksExpanding a list of parameters in Tcl - {*} and evalPerl - calls to methods that use => - what do they mean?Melksham Area - buses, trains, cycles, and roads. Meeting, 17.1.2012Transport in Cambridgeshire - seen by an outside observer. What can Wiltshire learn?A busy start to 2012