beetlebug▾
/

array names

Gives the names of the elements of an array.

array names ARRAYNAME ?MODE? ?PATTERN?

Description

Gives the name of each element, in the order in which the elements were made. With PATTERN, it gives only the names that match. MODE says how PATTERN matches: -glob (the default) with the rules of string match, -exact for the same text, and -regexp for a regular expression with the rules of regexp.

With one argument after ARRAYNAME, that argument is PATTERN, also when it starts with -: array names a -exact gives the elements with the name -exact. A variable that is not an array gives an empty list.

Arguments

ArgumentTypeDefaultDescription
ARRAYNAMEvariable namerequiredThe array to read.
MODEoption-globHow PATTERN matches a name.
ValueDescription
-globA glob pattern of string match.
-exactThe same text.
-regexpA regular expression of regexp.
PATTERNpatternnoneThe names to give, read by MODE.

Returns

A list of element names.

Examples

% array set n {b 1 a 2 ab 3}; array names n
b a ab

% array names n a*
a ab

% array names n -exact a
a

% array names n -regexp {^.$}
b a

# One argument is a PATTERN.
% array names n -exact

% lsort [array names n]
a ab b

Errors

% array names n -bogus x
bad option "-bogus": must be -exact, -glob, or -regexp
-errorcode TCL LOOKUP INDEX option

# Tcl 9.1 only
% array names n -regexp (
cannot compile regular expression pattern: parentheses () not balanced
-errorcode REGEXP REG_EPAREN {parentheses () not balanced}

# Tcl 8.6 only
% array names n -regexp (
couldn't compile regular expression pattern: parentheses () not balanced
-errorcode REGEXP REG_EPAREN {parentheses () not balanced}

% array names n -glob x y
wrong # args: should be "array names arrayName ?mode? ?pattern?"
-errorcode TCL WRONGARGS

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also