# SOLVEDMath operation array

#### Moltisanti

##### Member
Hi, I'm kinda new in GameMaker Studio and in programming in general, and I'm stuck with something that probably is a very simple thing.
If I have an array with multiple math operation elements (numbers and strings with symbols), like [8, "+", 9, "*", 3], can I somehow convert it into the actual operation? In this case it would be 8+9*3, so if I store it in a variable I want the value of that variable to be the result of the operation (51 in this case).
Thanks in advance!

#### chamaeleon

##### Member
There is no built-in way of doing this. You could look at converting it into a string and use some marketplace offering (N String Parser 2 or Apollo are two options) that performs evaluation of expressions. So in other words, it's not simple, in that you need to write code to perform everything, like parsing numbers, matching strings to operations, applying operations to the correct set of parsed numbers, etc. Obviously, any sane parser given your example would yield 35.

#### TailBit

##### Member
Find the operation with the highest priority, perform the operation on them, remove the three from the array and put the answer in their place .. repeat til done .. profit

#### Moltisanti

##### Member
There is no built-in way of doing this. You could look at converting it into a string and use some marketplace offering (N String Parser 2 or Apollo are two options) that performs evaluation of expressions. So in other words, it's not simple, in that you need to write code to perform everything, like parsing numbers, matching strings to operations, applying operations to the correct set of parsed numbers, etc. Obviously, any sane parser given your example would yield 35.
I've been trying to at least convert it into a string but I'm having trouble with that too. I didn't find any function like "join" in JavaScript, and functions like "string()" just give me the same array.

Last edited:

#### Moltisanti

##### Member
Find the operation with the highest priority, perform the operation on them, remove the three from the array and put the answer in their place .. repeat til done .. profit
I'll try something like that

#### Mr Magnus

##### Viking King
Use a for loop, iterate over the array, if it's an operator do the desired operation on the items before and after the operator in the array. Usually with these sorts of problems you'd employ a data-structure like a stack but since you're not doing any operation priority like PEDMAS you really just need a "result" variable counting it up as you loop trough.

Something like:

GML:
``````var length = array_length(equationArray);
var result = equationArray[0]; //First is a number
for(var i = 1; i < length; i += 2){ //You state that every other symbol is an operator, so we don't have to check the in-betweens.
var operator = equationArray[i];
switch(operator){
case "+":{
result += equationArray[i + 1]; //Add the number after the operator to the result.
break;
}
case "-":{
result -= equationArray[i + 1];
break;
}
case "*":{
result *= equationArray[i + 1];
break;
}
case "/":{
result /= equationArray[i + 1];
break;
}
}
}``````
This code *assumes* that there is always at least one thing in the array, and that it goes number, operator, number, operator, number e.t.c. It has no operation priority and just reads it left to right. The variable "result" has the result after you're done. If you need something more complicated the same ideas can work, but you'll need to adapt it or implement some parser to recognize what order to do what operations in.

#### Moltisanti

##### Member
Use a for loop, iterate over the array, if it's an operator do the desired operation on the items before and after the operator in the array. Usually with these sorts of problems you'd employ a data-structure like a stack but since you're not doing any operation priority like PEDMAS you really just need a "result" variable counting it up as you loop trough.

Something like...
Thanks, this is basically what I was trying to do. The problem now is that values are added to the array in real time so it crashes when the conditions are not met (for example, when a "+" is added but there is no number after yet). I'll try to fix it with conditionals.

EDIT: I added some conditionals and it appears to work just fine, thanks again!

Last edited: