By now, you've seen ObjectScript examples and written some code yourself, using variables. You've seen variables assigned both string and numeric values. Remember, there are no declaration statements in ObjectScript. ObjectScript is a dynamically typed, weakly typed language. All data (literals and variables) are interpreted properly based on the context in which they are used.
ObjectScript evaluates strings numerically when they are used in mathematical expressions. When assigning a variable a numeric value, you don't have to include quotes, as you must with a string that has non-numeric characters. In the example, ObjectScript interprets the string fred
as 0 for the purposes of adding 3 to fred
, because fred
has no leading numeric characters. By contrast, 32hut
is 32 when dividing it by 4. To simply see what the numeric interpretation of a string is, use the unary + operator. The final example simply illustrates that when the arithmetic operator (in this case, +) isn't within the quotes, it forces numeric interpretation, whether or not the operands are in quotes.
Remember that variables are case-sensitive, while commands are not.
SAMPLES>set x = 4 write x + 2
SAMPLES>set x = "5" write x * 2
SAMPLES>set x = "fred" write x + 3
SAMPLES>set x = "32hut" write x / 4
SAMPLES>write 5 + 2, ?10, "5" + "2" , ?20, "5 + 2"
7 7 5 + 2
© 1997-2019 InterSystems Corporation, Cambridge, MA