nstcl  >  Documentation  >  nstcl-misc  >  invert_array

Tcl Powered!



invert_array — Invert the contents of an array.


package require nstcl-misc ?1.0?
package require nstcl ?1.0?

::nstcl::invert_array ?-append? ?-split? ?-mode mode? old_var_name ?new_var_name?


Inverts an array. If foo(bar) == baz then the new array will have foo(baz) == bar. This assumes that both the keys and values are unique or that the caller is willing to live with lost data.


If specified, duplicate inverted values are appended as a list rather than being blindly overwritten. For example, if foo(bar) == 123, and foo(baz) == 123, and -append is specified then foo(123) == {bar baz} as opposed to either foo(123) == bar or foo(123) == baz.
If specified, values being split are treated as a list and each individual element of the list is added to the inverted array as a separate key. For example, if foo(123) == {bar baz}, and -split were specified, then foo(bar) == 123 and foo(baz) == 123 instead of foo(bar baz) == 123.
-mode (defaults to get)
This switch controls how the data is returned. get returns the inverted array in a format suitable for use with [array set]. update updates the contents of the old_var_name array in the callers enviroment, in place. new creates a new array, named new_var_name, in the callers enviroment. It is an error to specify this switch without providing a value for new_var_name, or to specify one with either -mode get or mode -update.


misc, nstcl