Version 2 of dict unset

Updated 2010-07-25 15:21:08 by dkf

Part of the dict command introduced in Tcl 8.5. See [L1 ] for documentation.

dict unset dictVarName key ?key?

[dict unset] removes one element from a dict or nested dict, given

  1. the name of the variable containing the dict, and
  2. the key or key path of the element to remove.

The variable is modified, and the new value is also returned. If the given key doesn't exist in the dict, no change is made. If a key path is given, all keys except the last must exist.

% # Simple example:
% set data {a 1 b 2}
a 1 b 2
% dict unset data a ;# Remove key "a" and its corresponding value "1"
b 2
% set data ;# Verify that variable is modified.
b 2
% dict unset data nonexistent ;# Check nonexistent key handling
b 2
% # Example using nested dicts:
% set data {row1 {col1 val11 col2 val12} row2 {col1 val21 col2 val22}}
row1 {col1 val11 col2 val12} row2 {col1 val21 col2 val22}
% dict unset data row2 col2 ;# Remove element "col2" in nested dict "row2"
row1 {col1 val11 col2 val12} row2 {col1 val21}
% dict unset data row1 ;# Remove element "row1"
row2 {col1 val21}
% set data ;# Verify again that variable is modified
row2 {col1 val21}
% dict unset data row2 col2 ;# Check nonexistent key handling
row2 {col1 val21}
% dict unset data row1 col2 ;# Check nonexistent nested dict handling
key "row1" not known in dictionary