0
votes

after doing some other things in my script I end up with a series of variables set in tcl ($sel1, $sel2, $sel3, ...) and I need to add them to the following line:

set all [::TopoTools::selections2mol "$box $sel1 $sel2 $sel3 $sel4"]

Now, if I only had four this would be fine by hand, but in the final version I will have hundreds which is untenable to do by hand. I'm sure the answer is some kind of loop, but I've been giving it some thought now and I can't quite figure it out. If I had, say, $sel1, $sel2, all the way to a given number, how would I add them to that line in the format shown at any amount that I want, with the $box at the beginning as shown? Thanks very much for your help.

It may or may not be relevant, but I define the variables in a loop as follows:

set sel$i [atomselect $id all]
1

1 Answers

2
votes

I'm not familiar with the software you are using, but it should be possible to fix this without too much hassle.

If you put this inside the loop instead:

set sell$i [atomselect $id all]
append valueStr " " [set sell$i]

(or perhaps this, even if it is little C:)

append valueStr " " [set sell$i [atomselect $id all]]

you will get the string that " $sel1 $sel2 $sel3 $sel4" is substituted into (remember to put $box in as well).

With Tcl 8.5 or later, you can do

dict set values $i [atomselect $id all]

inside the loop, which gives you a dictionary structure containing all values, and then create the sequence of values with:

set all [::Topotools::selections2mol [concat $box [dict values $values]]]

Depending on the output and input formats of atomselect and selections2mol, the latter might not actually work without a little fine-tuning, but it should be worth a try.

In the latter case, you aren't getting the variables, but each value is available as

dict get $values $i 

You can do this with an array also:

set values($i) [atomselect $id all]

but then you need to sort the keys before collecting the values, like this

set valueStr [list $box]
foreach key [lsort -integer [array names values]] {
    append valueStr " " $values($key)
}

Documentation: append, array, concat, dict, foreach, list, lsort, set