12.4.6 Using strings as commands: expr
The expr command lets you use a string as a command.
-
expr takes one argument:
str, a string which expresses a valid command.
- expr(str) converts str to
the command and evaluates it.
Examples.
-
Input:
Output:
- Input:
a:= "ifactor(54)" |
expr(a)
|
Output:
which is the same thing as entering ifactor(54) directly.
You can also use expr to convert a string to a number. If a
string is simply a number enclosed by quotation marks, then
expr will return the number.
Example.
Input:
expr("123")
Output:
In particular, the following strings will be converted to the
appropriate number.
-
A string consisting of the digits 0 through 9 which doesn’t start
with 0 will be converted to an integer.
Example.
Input:
expr("2133")
Output:
- A string consisting of the digits 0 through 9 which contains a
single decimal point will be converted to a decimal.
Example.
Input:
expr("123.4")
Output:
- A string consisting of the digits 0 through 9, possibly containing a
single decimal point, followed by e and then more digits 0
through 9, will be read as a decimal in exponential notation.
Example.
Input:
expr("1.23e4")
Output:
- A string consisting of the digits 0 through 7 which starts
with 0 will be read as an integer base 8.
Example.
Input:
expr("0176")
Output:
since 176 base 8 equals 126 base 10.
- A string starting with 0x followed by digits 0 through 9
and letters a through f will be read as an integer
base 16.
Example.
Input:
expr("0x2a3f")
Output:
since 2a3f base 16 equals 10815 base 10.
- A string starting with 0b followed by digits 0 and 1 will
be read as a binary integer.
Example.
Input:
expr("0b1101")
Output:
since 1101 base 2 equals 13 base 10.