array defaultTcl 9.1 only
Sets, reads, tests, or removes the value that a read of a missing element of an array gives.
array default set ARRAYNAME VALUE
array default get ARRAYNAME
array default exists ARRAYNAME
array default unset ARRAYNAME
Description
An array can have a default value. A read of an element that the array does not have gives the default value in place of an error, so incr and append on a missing element start from it.
setgives the array the default VALUE. It makes an empty array when ARRAYNAME has no variable.getgives the default value. An array with no default is an error.existsgives 1 when the array has a default, and 0 when it has none or when ARRAYNAME has no variable.unsetremoves the default. An array with no default is no error.
The default makes no element: info exists, array names, array get, and array size do not see it.
The subcommand is in Tcl 9.1 only.
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| ARRAYNAME | variable name | required | The array. |
| VALUE | string | required | The default value of set. |
Returns
The default value for get, 1 or 0 for exists, and an empty string for set and unset.
Examples
% array default set cnt 0; incr cnt(apple); set cnt(apple)
1
% set cnt(pear)
0
% array default get cnt
0
# The default makes no element.
% array size cnt
1
% array default exists cnt
1
% array default unset cnt; array default exists cnt
0
% array default exists nope
0
Errors
% array set h {}; array default get h
array has no default value
-errorcode NONE
% array default get nope
"nope" isn't an array
-errorcode TCL LOOKUP ARRAY nope
% array default bogus cnt
bad option "bogus": must be get, set, exists, or unset
-errorcode TCL LOOKUP INDEX option
% set plain 1; array default set plain 0
can't array default set "plain": variable isn't array
-errorcode NONE
% array default set cnt
wrong # args: should be "::tcl::array::default set arrayName value"
-errorcode TCL WRONGARGS
% array default get
wrong # args: should be "array default option arrayName ?value?"
-errorcode TCL WRONGARGS
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 only.
- Since
- ztcl 1.0.0-beta.1