Incrementing a value

Tcl provides a simple method for incrementing a value stored within a dictionary with the dict incr command. This is extremely useful when storing numeric values in the dictionary. The syntax is as follows:

	dict incr dictionaryValue key increment

How to do it…

In the following example, we will create a dictionary containing a key/value pair and then using the dict incr command, we will increase the value associated with the key specified. Return values from the commands are provided for clarity. Enter the following command:


% set numbers [dict create one 1]
one 1
% dict incr $numbers one 3
one 4

How it works…

The dict incr command will increase the value stored in key by the amount defined within the increment parenthesis for ...

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.