dict incr
Adds an integer to the value of a key in the dict of a variable.
dict incr VAR KEY ?INCREMENT?
Description
Adds INCREMENT to the integer value of KEY in the dict of the variable VAR, and gives the new dict. INCREMENT is 1 by default, and it can be negative or in any integer form, such as 0x10. A missing key starts at 0, and a VAR with no value starts as an empty dict. The sum has no size limit: it does not wrap at 64 bits.
The value of the key and INCREMENT must be integers.
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| VAR | variable name | required | The variable with the dict. |
| KEY | string | required | The key of the integer. |
| INCREMENT | integer | 1 | The amount to add. |
Returns
The new value of VAR.
Examples
% set d {n 1}
> dict incr d n
n 2
% set d {n 1}
> dict incr d n -2
n -1
% set d {}
> dict incr d m 0x10
m 16
% set d {n 1}
> dict incr d n 9223372036854775807
n 9223372036854775808
Errors
% set d {n x}
> dict incr d n
expected integer but got "x"
-errorcode NONE
% set d {n 1}
> dict incr d n 1.5
expected integer but got "1.5"
-errorcode NONE
Differences from Tcl
- For a missing key, Tcl 8.6.16 and 9.0.4 store INCREMENT as written:
dict incr d m 0x10on an empty dict givesm 0x10there (issue 661).
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1