Reputation: 2023
I am declaring a array in TCL say
set JDSU-12-1(key) element
parray JDSU-12-1
I am getting error saying JDSU is not a array
Even simple puts statement is not working
% puts $JDSU-12-1(key)
can't read "JDSU": no such variable
Is there any way i can declare array name with hyphen. I know _ works in array but not sure about hyphen
Upvotes: 2
Views: 1108
Reputation: 11
You can easily do that:
set set-var "test" while accessing so ${set-var}
Upvotes: 1
Reputation: 15202
Although you can use such special characters, you can only use a few when you try to access variables with $varname
.
To quote the relevant section from the manual:
$
nameName is the name of a scalar variable; the name is a sequence of one or more characters that are a letter, digit, underscore, or namespace separators (two or more colons). Letters and digits are only the standard ASCII ones (
0-9
,A-Z
anda-z
).
$
name(
index)
Name gives the name of an array variable and index gives the name of an element within that array. Name must contain only letters, digits, underscores, and namespace separators, and may be an empty string. Letters and digits are only the standard ASCII ones (
0-9
,A-Z
anda-z
). Command substitutions, variable substitutions, and backslash substitutions are performed on the characters of index.
${
name}
Name is the name of a scalar variable or array element. It may contain any characters whatsoever except for close braces. It indicates an array element if name is in the form “arrayName(index)” where arrayName does not contain any open parenthesis characters, “
(
”, or close brace characters, “}
”, and index can be any sequence of characters except for close brace characters. No further substitutions are performed during the parsing of name.There may be any number of variable substitutions in a single word. Variable substitution is not performed on words enclosed in braces.
Note that variables may contain character sequences other than those listed above, but in that case other mechanisms must be used to access them (e.g., via the set command's single-argument form).
I want to empathis the last paragraph a bit:
You are always able to read any variable with set varname
:
set JDSU-12-1(key) element
puts [set JDSU-12-1(key)]
Unlike the ${varname}
access, you can substitute a part of the variable name (in your case the array key), the entire variable, while set k "key"; puts ${JDSU-12-1($k)}
does not work.
Upvotes: 3
Reputation: 137807
You can use almost any character for the name of a variable in Tcl — the only restrictions relate to ::
as that is a namespace separator, and (
as that is used for arrays — but the $
syntax is more restrictive; the name it accepts (without using the ${…}
form) has to consist of just ASCII letters, ASCII digits, underscores or namespace separators. Dashes aren't on that list.
The standard (and simplest) way of reading from a variable with a “weird” name is to use set
with only one argument, as that happily accepts any legal variable name at all:
puts "the element is '[set JDSU-12-1(key)]'"
However, if you're doing this a lot it is actually easier to make an alias to the (array) variable name:
upvar 0 JDSU-12-1 theArray
puts "the element is $theArray(key)"
That's exactly how parray
does it, though it uses upvar 1
because it is aliasing to a variable in the calling scope and not in the current scope.
Upvotes: 3
Reputation: 519
Which version are you working ?? my tcl works.
% set JDSU-12-1(key) element
element
% parray JDSU-12-1
JDSU-12-1(key) = element
Upvotes: 0
Reputation: 71598
You can use special characters in Tcl variable names. You need the braces for those though:
% puts ${JDSU-12-1(key)}
element
You can even use $
:
% set \$word "Hello world" ;# Or set {$word} "Hello world"
% puts ${$word}
Hello world
EDIT: Some reference:
beedub.com (Emphasis mine)
The set command is used to assign a value to a variable. It takes two arguments: the first is the name of the variable and the second is the value. Variable names can be any length, and case is significant. In fact, you can use any character in a variable name.
Upvotes: 4
Reputation: 116457
Like in most other programming languages, TCL variable must be alphanumeric starting with letter (A
to Z
, or _
). Hyphen or dash (-
) is not permitted as part of variable name, otherwise it would be confused with arithmetic minus or subtraction: there would be no difference between $x-1
as variable with name "x-1"
or $x-1
as variable x
minus 1
.
Upvotes: 0