From: Bob H. <ha...@st...> - 2007-03-02 20:50:58
|
Molecular Math in Jmol -- Part IV: Item Selectors [n][m] and @ substitution A third capability now part of the Jmol query language is the ability to select items from a set. This is done using one or two bracketed numbers after the set. The sets include four types: atom sets {oxygen}[2] bond sets {carbon}.bonds[1] strings "this is a test[1][4] string arrays {carbon}.bonds.ident.lines[1] The idea is simply this: the first number in brackets selects an item. If there are two brackets, then a range of items is selected. The special second item [0] means "and all after that". In addition, since there is automatic conversion to strings, you can do some unusual selections. Any number is first turned into a string when using string-related functions such as .find(), .replace(), or .split(). x = 123.456; x = x[1][x.find(".")-1] #returns the string "123" x = 123.466.split(".")[1] # same thing -- probably better x = 123.456; x = 0 + x[1][x.find(".")-1] #returns the number 123 x = 0 + y.replace("-","") # returns the absolute value of y as a number @ variable substitution ----------------------- The idea is that you have a variable and you want to use it in a command. There are some tricks to using this -- it won't work in all situations, mostly because I haven't taken the time to make sure it will. But the idea is this: I want the wireframe to change just using "x". Simple: x = 0.20; wireframe @x xb = false;wireframe @xb x = {2 2 2}; draw point1 @x y = x + {1 0 0}; draw line1 @x @y So that's pretty straightforward. This does not work in strings. You cannot say: x = 3;label " testing @x " But you can say: x = "testing " + 3; label @x So the quickest way to see the value of a variable is simply to use "message @x". For example: x = data({selected},"XYZ"); message @x [[XYZ format file data is displayed]] Now, here's where it gets interesting: You can use @x in the SET command. The result is that it isn't x that gets set, it's the variable NAMED by x that gets set. This allows the idea of an array. So, for example: x = test set @x = 3 This is the same as test = 3 In 11.1.16 that "set" is REQUIRED, but in 11.1.17 you will be able to just use: @x = 3 By itself, not very useful. But consider this: y = 1 atomy = "atom"+y set @atomy {*}[y].ident Now the variable "atom1" is the identity of atom 1. So there's the essence of an array. You can now say: str = "The identity of atom "+ y +" is " + @atomy Of course, an easier way to do this is just str = "The identity of atom "+ y +" is " + {*}[y].ident but the advantage of using an array variable is that you can then track all sorts of things, not just identities. There are definitely occasions where this is useful. To be continued.... ---------- Bob |