# How to calculate the sum of a list of integer in Rules

In a Rules component I have a list of integer (as variable data type). I have to calculate the sum of items and to expose the result. Any suggestions?

• I've been a big user of rules, so I could be wrong, but I don't think there are a whole lot of arithmetic support in general in rules. I believe you'd need to write your own rule for this. (Which could btw be an excellent addition to contrib) Commented Sep 22, 2013 at 11:13
• Hey @Letharion , if you are ever around on drupal.SE again, I'd be interested in your feedback/review of my answer to this question, which is about a field in a field collection (= variation of the list of integers in this question). Commented Nov 22, 2016 at 15:04
• @Pierre.Vriens I'm honored you'd seek out my opinion specifically. :) Sadly I work very little with Drupal these days, and I fear I can't add much to your answer. It looks rather well written as it is. :) Commented Nov 22, 2016 at 20:50
• @Letharion : merci for your feedback ... anyway! Commented Nov 23, 2016 at 12:25

I found the solution.

1. Add a variable (integer) "total", for example.
2. Loop the list of integers:

• Calculate a value, input 1: `[total]`, input 2: `[line-item]`, return: "result".
• Set a data value, data: `[total]`, value: `[result]`.
3. Outside of the loop, the `[total]` value is still available.

this is my suggestion, create the following function in your template file:

``````function <theme>_preprocess_node(&\$variables)
{

}
``````

and by kpr() function from devel module try to catch where these outputs are positioned in the page array. now you can take them by a foreach loop and cast them to integer.

• I don't believe that helps the OP, as the sum should be useful in a rule, not just for display. Commented Sep 22, 2013 at 11:11