[REBOL] Re: let's re-visit math oper. in a var.
From: gregg:pointillistic at: 3-Apr-2009 10:02
Hi Duke,
DN> What's wrong _now_........?
Remember, REBOL doesn't evaluate strings they way you might think it
does. You can DO a string, and you should play with that, but they are
not implicitly evaluated. So, when you do this:
DN> op: ask "Enter an operator [+ - * /]: "
DN> op2: join ":" :op
The resulting op is just a string with a colon on the front.
Enter an operator [+ - * /]: +
== ":+"
If you DO the resulting string (":+"), REBOL will, indeed, convert it
to an op, but you're not doing that in your code, you're code is doing
this:
print reform ["result 1: " (to-integer num1) ":+" (to-integer num2)]
Note here that there is an important distinction between DO and
REDUCE.
>> s: ":+"
== ":+"
>> reduce s
== ":+"
>> do s
>> type? do s
== op!
When working with REBOL, if things aren't working as expected, it's a
good idea to check the type of value you're operating on, and to MOLD
them when printing them for analysis.
>> print "+"
+
>> print '+
+
>> print #+
+
>> print mold "+"
+
>> print mold '+
+
>> print mold #+
#+
clrscrn: does [print "^(1B)[2J"]
until [
clrscrn
num1: ask "Enter the 1st number: "
while [error? try [num1: to-integer num1]] [
print ["Sorry," mold num1 "doesn't look like a number to me"]
num1: ask "Enter the 1st number: "
]
num2: ask "Enter the 2nd number: "
while [error? try [num2: to-integer num2]] [
print ["Sorry," mold num2 "doesn't look like a number to me"]
num2: ask "Enter the 2nd number: "
]
op: ask "Enter an operator [+ - * /]: "
while [not find "+-*/" trim op] [
print ["Sorry," mold op "doesn't look like one of the operators I know"]
op: ask "Enter an operator [+ - * /]: "
]
;op: get to word! op ; try this, to see why it doesn't work
op: get select ["+" add "-" subtract "*" multiply "/" divide] op
print reduce ["result 1: " op num1 num2]
confirm "Are you done? Y/N: "
]
-- Gregg