aitcs(n) 1.7 aitcs "AitCS"

Name

aitcs - Array in the Command's Shadow

Table Of Contents

Synopsis

Description

An Array in the Command's Shadow is an array with an associated set of commands that each provide access to or allow certain useful manipulations of the array.

An AitCS may be used where a program would normally use global or namespace variables to hold configuration, state, widget paths, temp values, etc.

Arrays are named+randomly-named and created in a namespace. Several commands (via aliases) are created in a (possibly different) namespace to manipulate the array.

The name ends up being used much like the set command.

The inspiration came from being tired of having to use the variable command in almost all the procs in a namespace when often in a given proc there is interest only in a few items in the global state.

By reducing array manipulation clutter this extension may help in improving program clarity!

COMMANDS

aitcs name name ?variable_namespace?

Names a new array in the namespace variable_namespace. The format of the name is "namespace::__name__cccccc_" where "cccccc" is six random characters in the range a-z. If variable_namespace isn't supplied or is an empty string then the array will be named in the caller's namespace. Doesn't create an array variable. Returns the name of the newly named array.

aitcs new ?name? ?command_namespace? ?variable_namespace? ?key val...?

Creates a new array in the namespace variable_namespace and creates commands (via aliases) in the command_namespace to manipulate the array. The format of the name and the variable_namespace will be as described in the name ensemble subcommand. If the name isn't supplied or is an empty string then the name will be set to a string of six random characters in the range a-z. If the command_namespace isn't supplied or is an empty string then the command_namespace will be set to the variable_namespace. The commands (via aliases) are created in the command_namespace. Initial values are set from key val... if supplied. See the AITCS COMMANDS section. Creates an array variable in the variable_namespace. Returns the name of the newly named array, or the randomly-generated name if the name argument wasn't supplied.

AITCS COMMANDS

Commands (via aliases) are created in the command_namespace and are based on the name supplied when the array was created.

name!

Causes the array to come into existence in the current context. This is upvar #0 varname name. From then on in the current context the array can be referenced by name. Returns an empty string. Nickname: Make Exist.

name key ?val? ?key val...?

Gets the value of one element of the array or sets the value of one or more elements of the array. The value(s) for the key(s) will be set using array set. Returns the value at key. Nickname: Get/Set.

name? key ?myVar?

Returns true if the key exists in the array or false if the key doesn't exist in the array. If the key exists and myVar is supplied and is not an empty string then the variable it names will be set in the caller's context to the value at key. Nickname: Exists.

name- key ?key ...?

Removes key(s) from the array. The array unset command is used, allowing key(s) to contain wildcards. It isn't an error if a key doesn't exist in the array. Returns an empty string. Nickname: Unset.

name^ key ?default? ?myVar?

Returns the value at key if it exists, otherwise returns default, or an empty string if default isn't supplied. If myVar is supplied and is not an empty string then the variable it names will be set in the caller's context to the returned value. Nickname: Get With Default.

name& ?key?

Returns the fully namespace qualified name of the array if no key supplied or if key is an empty string, otherwise returns the fully namespace qualified name of the array and the element key. Nickname: Variable Reference.

name&&

Returns the fully namespace qualified command. Nickname: Command Reference.

name= key ?key ...? val

Every key is set to val. Returns val. Nickname: Setto.

name+ key ?val ...?

Every val is lappended to the element at key. Returns the value at key. Nickname: Lappend.

name+= key ?incr?

Increments the value at key by incr (default 1). Returns the value at key. Nickname: Incr.

name. key?op? ?arg ...?

The key should be another AitCS. Permits "nesting". Calls are chained. Nickname: Struct Dereference.

name---

Unsets the array and deletes all the commands. Returns an empty string. Nickname: Destroy.

EXAMPLES

	# Set/Get/Variable Reference
	# Save a widget and link a textvariable
	aitcs new rec
	aitcs new gui
	set fld preference
	rec $fld Tcl
	gui $fld [entry .e -textvariable [rec& $fld]]
	[gui $fld] insert end /Tk
	string length [rec preference]; # 6
	# Get/Set
	aitcs new things
	things a 3
	things b [expr {[things a] + 2}]
	things c [expr {[things b] * [things a]}]
	things c; # 15
	# Exists
	aitcs new colours
	colours red #FF0000
	colours? red; # 1
	colours? green; # 0
	colours? red color1; # 1
	info exists color1; # 1
	set color1; # #FF0000
	colours? green color2; # 0
	info exists color2; # 0
	# Setto/Exists/Unset/Variable Reference
	aitcs new fruits
	fruits= apple orange pear apricot 0
	if {![fruits? peach]} {
		fruits= peach 0
	}
	array names [fruits&]; # {apple orange pear apricot peach}
	fruits- p* a*
	array names [fruits&]; # {orange}
	# Get With Default
	aitcs new things
	things are good
	things^ are ok; # good
	things^ be ok; # ok
	things^ willbe; # Empty string
	things^ is nice here; # nice, and var here is set to nice
	# Lappend
	aitcs new store
	store this one
	store+ this two three four five
	store this; # {one two three four five}
	# Incr
	aitcs new counters
	counters cycles 0
	counters+= cycles
	counters+= cycles 2
	counters+= cycles -1
	counters cycles; # 2
	# Make Exist/Exists
	aitcs new cow
	cow!
	set cow(sound) moo
	if {[cow? sound] && $cow(sound) eq "moo"} {
		puts "Standard English Cow"
	}
	# Command Reference/Variable Reference/Incr
	aitcs new callback
	callback count 0
	after 1000 [list [callback&&]+= count]
	vwait [callback& count]
	callback count; # 1
	# Struct Dereference/Random Name/Initial Values
	aitcs new animals {} {}  dog [aitcs new {} {} {} says woof likes cat]  cat [aitcs new {} {} {} says meow likes dog]
	animals home [list dog cat]
	set res ""; foreach animal [animals home] {
		append res $animal " says " [animals. $animal says]
		append res " likes " [animals. $animal likes]
		append res " who says " [animals. [animals. $animal likes] says]\n
        }
	set res
	# dog says woof likes cat who says meow
	# cat says meow likes dog who says woof
	# Struct Dereference
	aitcs new outer
	aitcs new inner
	aitcs new innest
	inner count 6
	innest things [list]
	innest+ things 1 2
	outer member inner
	inner item innest
	outer. member+= count 2; # 8
	outer. member. item+ things 3 4; # {1 2 3 4}
	outer. member. item^ name; # Empty string
	# Destroy
	aitcs new tmp
	tmp count 5
	tmp+= count -1
	tmp count; # 4
	set fqarr [tmp&]
	info exists $fqarr; # 1
	tmp---
	info exists $fqarr; # 0
	tmp count; # <error>

Keywords

alias, array, command, eagle, namespace, snake

Category

Programming tools