TIP 224: Add New [array] Subcommands 'incr' and 'value'

Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2017 Conference, Houston/TX, US, Oct 16-20
Send your abstracts to tclconference@googlegroups.com
by Aug 21.
Author:         Peter MacDonald <peter@pdqi.com>
Author:         Robert Seeger <rseeger00@aol.com>
State:          Draft
Type:           Project
Vote:           Pending
Created:        28-Sep-2004
Tcl-Version:    8.7


The TIP proposes the addition to two new subcommands to the array command: incr and value. The incr_ subcommand would increment an element in an array. The **value subcommand would query, reference, and/or initialize an array element.


[215] proposed modifying the incr command to auto-initialize variables which do not exist, rather than throwing an error. After some debate, it was identified that frequency counting in arrays was the primary area of interest. In particular, the excessive use of if/info exists constructs in such code is inefficient, verbose and rather difficult to read. The addition of these two subcommands could substantially reduce the complexity of much Tcl code.


Two new subcommands will be added to array, being incr and value. Both commands take an array name and an elem parameter, but, no string match is performed on elem.

array incr

array incr var elem ?value? ?init?

The array incr subcommand would increment var(elem), if it exists, by value, otherwise, initializes it to init. The default for value is 1, and the default for init is 0. The subcommand will return the new value and will support both integer and double values (based on the rules for addition in expr).

array value

array value var elem ?value? ?init?

The array value would just return the current contents of var(elem) if it exists, or value otherwise. If the init parameter resolves to true (as determined by Tcl_GetBooleanFromObj() of course), the variable is initialized to value if it doesn't already exist. The default for value is the empty string, and the default for init is false.

Reference Implementation

Following is a Tcl only implementation of the incr/value subcommands:

proc Array {cmd var elem {amt {}} {init 0}} {
   # Implement the Array subcmds incr and value on var(elem):
   #  - incr   increment a variable by amt or initialize
   #           to init if undefined.
   #  - value  return the value if defined, else return amt
   #           initializing if init.
   upvar $var uvar
   if {[string match $cmd* incr]} {
      if {$amt == {}} { set amt 1 }
      if {[info exists uvar($elem)]} {
         return [set uvar($elem) [expr {$uvar($elem)+$amt}]]
      return [set uvar($elem) $init]
   } elseif {[string match $cmd* value]} {
      if {[info exists uvar($elem)]} {
         return $uvar($elem)
      if {$init} {
         return [set uvar($elem) $amt]
      return $amt
   } else {
      error "usage: Array incr|value var elem ?amt? ?init?"



This document has been placed in the public domain.