How to Sum All the Lines in a Variable?

I'm sure there is an easy answer to this, so forgive me the simple question, but how do I add up all the values in a var?

What I did was set up a function to copy a blob of text and put that in a var. I then used regex to remove all text except the numbers. Now I want to add up all the numbers and put the sum on the clipboard. But I can't figure out how to add up all the numbers contained in that var. Any help?

Is the variable in the form of a list or an array? Which one will determine the appropriate solution.

The simplest way to proceed is for you to show an example of the contents of the variable by posting it here.

Ok, thanks for answering. I didn't put them in an array (I wouldn't know how). They are in whatever format the clipboard gave. When I run what I have, I am left with this in the var:

  17.383 
          19.105 
      17.329 
  16.157 
      18.744 
          18.169 
      18.812 
              21.195 
  22.463 
  21.245 
      23.299 
      22.280 
              24.021 
    24.538 
  26.354 
  27.105 
      32.186 
      30.104 
  28.115 
  26.390 
  26.397

Here you go. The actions coloured green in this macro do the hard work. I hope you can adapt it to your needs.

Test: Sum all Numbers in a Variable.kmmacros (4.2 KB)

KM 0 2021-10-06_18-01-23

1 Like

Thank you!!

1 Like

You’re welcome. If you come across other KM problems you can always ask for help in this forum!