| The same concept holds for multidimensional lists,
but care must be taken that the list indices do not interfere, best by separating them with an underscore
'_':
for i=1 to 12
for j=1 to 12
wrongmatrix(i)(j)=i*j
rightmatrix(i)_(j)=i*j
Why is the first matrix wrong? Simply because the matrix elements
1,12 and 11,2 (like several others) map to the same variable name: wrongmatrix112. The underscore makes sure that the indices stay separated: rightmatrix1_12 and rightmatrix11_2 are indeed different names.
If you know the number of columns and rows in advance, you can use leading zeroes to avoid the underscore:
for i=01 to 12
for j=01 to 12
rightmatrix(i)(j)=i*j
In the above case, rightmatrix0112 and rightmatrix1102 correctly map to different variable names.
As lists are just variables with the same root name and a sequential number at the end,
the length of a list can be determined by counting the number of names that match a certain pattern. This is done by the
'count' function:
MyList() = 4,8,5,2
for i = 1 to count MyList
Print 'Element (i): (MyList(i))'
Element 1: 4
Element 2: 8
Element 3: 5
Element 4: 2
If no variable name matches, 'count' returns 0 to indicate that the list is empty
/ does not exist. These five functions scan all variable names that match the given
root name and return the smallest, largest,
summed up and average value, respectively. The datatype of the result is either an integer or float,
depending on the list elements. The standard deviation is calculated using the formula:
StdDev^2 = Sum((ElementX-Mean)^2) / Elements
Example for a simple list:
MyList() = 1,4,-5,10,8,2
Print (count MyList)
Print (min MyList)
Print (max MyList)
Print (sum MyList)
Print (mean MyList)
Print (stddev MyList)
Print (join MyList)
6
-5
10
20
3.33333333333333e+00
4.88762609953839e+00
1 4 -5 10 8 2
The Yanaconda language does not contain a single function to sort a list, since most of the time one wants to sort something else based on the numbers stored in a list.
The following example assumes that the simulation cell contains one protein multiple times in
different, non-overlapping conformations
. This macro sorts these conformations based on their energy (the cell is assumed to be the last object):
energylist() = EnergyObj all
do
sorted=1
for i=1 to Objects-2
if energylist(i)>energylist(i+1)
SwapObj (i),(i+1)
swap=energylist(i)
energylist(i)=energylist(i+1)
energylist(i+1)=swap
sorted=0
while not sorted
|