- [[crpl:crplreference| CRPL reference]] <- [[crpl:crplreference#comparators|Comparators]]
===== eq =====
^Arguments^Result^Notation^
|Two Values|true or false (1 or 0) |''n1 n2 -- n3 ''|
=== Description ===
Top two items are popped from the stack and 'Equal'
comparison is performed. 0 or 1 is pushed back to the stack where 1 indicates true.
=== Comparing unlike types ===
When trying to do a comparison where one argument is a string and the other is an int, the string gets coerced into an int. If the string is something like "42" that coerces into 42. If it is "abc" that coerces into 0.
In particular, if x may be a string or number, (<-x "abc" eq) will unexpectedly return true when x equals 0. One remedy is to coerce x to a string before comparing as in (<-x "" concat "abc" eq).
The 'eq' (and other comparators) look at both arguments and their intrinsic types. The rules are:
- If either argument is a floating point number, then treat both arguments as floating point numbers, coercing the other argument if necessary.
- If both arguments are strings, then do string comparisons. So, EQ would compare strings, for instance.
- If neither 1 nor 2 above is true, then treat each argument as an int, coercing the arguments if necessary.
=== Examples ===
# Compares numbers to 100.
# Pointless, in programming, but here to illustrate a point.
100 50 gt # True, 100 is greater than 50
45 50 gt # False, 45 is not greater than 50
50 50 gt # False, 50 is not greater than 50
Examples of comparing with unlike types
"string" ->string
0 ->zero
1 ->one
Trace(<-string eq(<-one)) # returns 0/FALSE
Trace(<-string eq(<-zero)) # returns 1/TRUE
"1" ->OneAsString
Trace(<-OneAsString eq(<-one)) # returns 1/TRUE
Trace(<-OneAsString eq(<-zero)) # returns 0/FALSE
# If core doesn't have Script.crpl attached, GetScriptVar will return 0.
# Then "MyScript" is coerced to 0 and the result is TRUE.
Trace(GetScriptVar(<-core "Script.crpl" "name") "MyScript" eq)
# The following version returns TRUE only if core has Script.crpl attached with
# a variable "name" whose value is the string "MyScript".
Trace(GetScriptVar(<-core "Script.crpl" "name") "" concat "MyScript" eq)