Tcl variables (with either scalar or array data) can be used in the selection in one of two possible ways.
If a keyword starts with a $, the rest of the keyword is used as the Tcl variable name. The Tcl interpreter is queried for the associated data, which is then used as it would be for any other keyword. The variable can be either an array, in which case element 0 is for the first atom, 1 for the second, ...; or a scalar value, in which case that value is used for all the atoms. For an array example, suppose the Tcl array ``foo'' contained the values foo(n) = 2*n. Then the statement $foo < 10 selects atom with indicies 0 to 4, while $foo 20 to 30 6 selects indicies 10, 11, 12, 13, 14, 15, and 3. If foo has a scalar value, such as PRO, the statement resname eq $foo selects the proline residues. Because of a limitation in Tcl, the only way VMD recognizes if a variable is an array is if element 0 exists.
The other way to reference a Tcl variable is as a boolean array. This is most often used when there is a set of atoms which must be referenced often or when the selection is very complicated. These start with a @ sign and specify if the given atom should be on or off. This notation is used in the same place as a boolean keyword (like protein or water). As with the $ variables, the Tcl variable can be either a scalar or array. If the value is false, (or 0) the corresponding atom is not selected. If true (or non-zero), it is turned on. Additionally, if the scalar value is an atom selection, the data from that selection is used. Here are some examples of @variables:
if the array foo(n) is defined as 1 of n is odd, 0 if n is even, then the selection text @foo turns on atoms with an odd numbered atom index.
Let ``my_selection'' be defined as set my_selection [atomselect top resid 35] then the selections ``$my_selection and name CA'' and ``sidechain $my_selection'' are also valid.
The difference between the two notations ($ and @) is somewhat subtle and occurs because the original selection language is not as dynamic as one would expect. The key thing to remember about Tcl references is they are used only where keywords are allowed. $variables are for keywords that have a value while @variables are for boolean keywords. That means variable references cannot be in the parameter list for the value keywords (eg, resname $name is not allowed). Instead, the comparison operators must be used (as in resname eq $name).