Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Expanding a list of parameters in Tcl - {*} and eval

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 as below, or enter http://melksh.am/nnnn for individual articles
T206 - Tcl/Tk - Lists
  [4455] Working out distance between places, using OS grid references and a program in Tcl - (2015-03-11)
  [4454] Everything is a string - even a list - (2015-03-11)
  [4209] Lists in Tcl - fundamentals in a commented source code example - (2013-11-16)
  [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)
  [1334] Stable sorting - Tcl, Perl and others - (2007-09-06)
  [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)

T214 - Tcl/Tk - Other Facilities in Tcl
  [4762] Coverage map in Tcl - how many times has each proc been called? - (2017-09-28)
  [4525] What does Tcl do if you try to run a command that is not defined? - (2015-10-10)
  [4523] Catching failed commands and not crashing the program in Tcl - (2015-10-10)
  [4207] Exception handling in Tcl - (2013-11-14)
  [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)
  [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)


Back to
Tcl collections - lists, dicts and array
Previous and next
or
Horse's mouth home
Forward to
QR codes - graphics images that provide quick phone links
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 service
The changing Hotel and Bed and Breakfast business in Melksham
QR codes - graphics images that provide quick phone links
Expanding a list of parameters in Tcl - {*} and eval
Perl - calls to methods that use => - what do they mean?
Melksham Area - buses, trains, cycles, and roads. Meeting, 17.1.2012
Transport in Cambridgeshire - seen by an outside observer. What can Wiltshire learn?
A busy start to 2012
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3583_Exp ... -eval.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat