I'm working on something now that uses a fair bit of maths and array manipulation (yes, it's another puzzler!).
Is there a way of using a wildcard as an operator eg
rather than typing -
If operator=1 Then a=5*3*4
If operator=2 Then a=5+3+4
If operator=3 then a=5-3-4
... etc
I'd rather something that was more like
#=*
a=#5#4#3
All that changes is the operator code. In my actual game the values are all arrays, so it's quite a bit longer to type.
I should probably know this, but I've been up all night coding and got to a point where "Brain says 'No'" Little Britain stylee.
Would a Switch/Case statement block work for you in this case and then just call functions for doing the math?
I could just create a function to do it, but the SELECT/CASE would be terribly long winded - I have 22 possible arrays to check and each can have upto 5 operations (+,-,*,/ and possibly =, but not necessary) for upto 36 possible objects. I only need to check them all once, so it's not processor intensive.
I can do a function pretty easily, but I was hoping for something quicker than that.
If you ALWAYS have 3 values with 2 operators (or something similar) then you could create a function like:
CONSTANT OPERATOR_ADD% = 1
CONSTANT OPERATOR_SUBTRACT% = 2
CONSTANT OPERATOR_MULTIPLY% = 3
CONSTANT OPERATOR_DIVIDE% = 4
FUNCTION Evaluate#: v1#, op1%, v2#, op2%, v3#
LOCAL rv#
rv = Operator(v1, v2, op1)
rv = Operator(rv, v3, op2)
RETURN rv
ENDFUNCTION
FUNCTION Operator#: v1#, v2#, operator%
SELECT operator
CASE OPERATOR_ADD; RETURN v1 + v2
CASE OPERATOR_SUBTRACT; RETURN v1 - v2
CASE OPERATOR_MULTIPLY; RETURN v1 * v2
CASE OPERATOR_DIVIDE; RETURN v1 / v2
ENDSELECT
ENDFUNCTION
//Usage:
answer = Evaluate(num1, OPERATOR_SUBTRACT, num2, OPERATOR_DIVIDE, num3)
Or use the last function, Operator() to suit your needs. Warning, this code has left to right order of operation, not standard.
Or, I've done this in another language (can't remember which) that just takes an expression as a string, and steps through it one character at a time and looks for start / end of a value, ignores spaces and invalid characters, and detects operators and applies them to the values as expected. Not that hard, but you may make it easier to having assumptions with the input string such as using a delimiter to separate the items such as "12|*|4|+|2" so you can use the split string command.
Cheers Slydog, that's pretty much what I had planned to do. :)