Appending to an existing record

To append data to an existing value Tcl provides the dict lappend command. The syntax is as follows:

	dict lappend name key value…

How to do it…

In the following example, we will create a dictionary containing a collection of key/value pairs and then using the dict lappend command we will append items to the key referenced. Return values from the commands are provided for clarity. Enter the following command:


% set test [dict create 1 1 2 2 3 3]
1 1 2 2 3 3

% dict lappend test 2 more
1 1 2 {2 more} 3 3

How it works…

The dict lappend command appends the data stored in value for the key referenced in key for the dictionary specified in name.

Get Tcl/Tk 8.5 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.