# How to sum variables?

## Summing variables

One method for summing a number of variables is to use the addition operator

**1 + 4 + 7 = ? m (12 m)**

An alternative method is to use the sum function

**Sum( 1, 4, 7 ) = ? m (12 m)**

You can of course also sum variables

**L1 = 1 m
L2 = 4 m
L3 = 3 m
Sum( L1, L2, L3 ) = ? m (8 m)**

In the above example there are known to be three values for L[n] But how can you write the expression so that it will work for any number of L values, i.e. 1-n?

You could use the GetVar function to ensure that if a particular L value didn't exist a value of zero is used so as not to affect the end result, the example below will correctly sum the values of L[n] for 1, 2 or 3 values, however it is still limited to a maximum of the specified variables.

**Sum( GetVar("L1", 0m), GetVar("L2", 0m), GetVar("L3", 0m) ) = ? m (8m)**

To write an expression that will sum any number of variables in a series you have to write a sequence of expressions that will create a string of the expression and then evaluate that string

**NumItems = 3**

Eval("Sum( L[1] )", StrReplace( StrListRange(NumItems), ",", ",L" )) = ? m (8m)

Eval("Sum( L[1] )", StrReplace( StrListRange(NumItems), ",", ",L" )) = ? m (8m)

NumItems is a variable that defines how many items there are to sum from 1-n.

How it works

- Create a comma separated list of the indices of the variables to sum

**StrListRange(NumItems)= "1,2,3"**

- Replace all the commas in the string with ,L

**StrReplace( "1,2,3", ",", ",L" ) = “1,L2,L3”**

- Insert the string in to the string “Sum( L_{[1]} )” and evaluate that string to get the final result

**Eval("Sum( L[1] )", “1,L2,L3" )) = 8m**

To amend the sum expression for different variables change the text in red as appropriate for the variable name you need.

**L1, L2, L3, L4, …
Eval("Sum( L[1] )", StrReplace( StrListRange(NumItems), ",", ",L" )) = ?**

**L_{1}, L_{2}, L_{3}, L_{4}, …**

Eval("Sum( L_{[1]} )", StrReplace( StrListRange(NumItems), ",", "},L_{" )) = ?

Eval("Sum( L_{[1]} )", StrReplace( StrListRange(NumItems), ",", "},L_{" )) = ?

**A_{1}, A_{2}, A_{3}, A_{4}, …**

Eval("Sum( A_{[1]} )", StrReplace( StrListRange(NumItems), ",", "},A_{" )) = ?

Eval("Sum( A_{[1]} )", StrReplace( StrListRange(NumItems), ",", "},A_{" )) = ?

One final improvement to the use of the sum expression is to assign the expression to an expression variable. An expression variable stores the actual expression rather than the result of the expression, this allows you to easily reuse the expression variable as if it were a function. To specify that the value of a variable should be stored as an expression prefix the variable name with the $ symbol.

**$L_{sum} = Eval("Sum( L_{[1]} )", StrReplace( StrListRange(NumItems), ",", "},L_{" ))**

L_{1} = 1 m

L_{2} = 4 m

L_{3} = 3 m

NumItems = 1

L_{sum} = ? m (1 m)

NumItems = 3

L_{sum} = ? m (8 m)

L_{2} = 8 m

L_{sum} = ? m (12 m)

L_{1} = 1 m

L_{2} = 4 m

L_{3} = 3 m

NumItems = 1

L_{sum} = ? m (1 m)

NumItems = 3

L_{sum} = ? m (8 m)

L_{2} = 8 m

L_{sum} = ? m (12 m)