• ## Outreach

From: Axel Kohlmeyer (akohlmey_at_gmail.com)
Date: Mon Jul 03 2017 - 14:19:13 CDT

On Sat, Jul 1, 2017 at 9:12 PM, MD Simulation
<mdsimulationgroup_at_gmail.com> wrote:
> Hello,
>
> I'm trying to write a procedure that will determine the empirical formula of
> a fragment:
>
>> proc get_empirical_formula {frag_number} {
>> set frag_sel [atomselect top "fragment \$frag_number"]
>> set atoms [\$frag_sel get element]
>>
>> set empirical_formula [dict create]
>> foreach atom \$atoms {
>> if {[dict exists \$empirical_formula \$atom]} {
>> set value [expr {[dict get \$empirical_formula \$atom] + 1}]
>> dict set \$empirical_formula \$atom \$value
>> } else {
>> dict set \$empirical_formula \$atom 1
>> }
>> }
>> puts \$empirical_formula
>> }
>
>
> The idea is that when you get the elements of the frag_sel, you'll get a
> list, for instance "C C H H H H H H". I then try to foreach the atom list
> and increment a dictionary value. When I try to print the dictionary, all I
> get is empty.

you could do a simpler strategy. when you get the list of elements,
also sort it.
then define a "hold" variable and initialize it to a dummy element
(XXX), also define a counter and have a loop over the list of sorted
elements.
whenever the current element is different from the hold element (and
the hold element is different from the dummy value), output the
counter. then initialize the counter to 1 and set the hold element to
the current element. when the current element is the same as the hold
element, just increment the counter. when you reach the end of the
list, print the current counter value. with that, you should easily
```--