Vishwadeep Singh
Vishwadeep Singh

Reputation: 1043

expr is giving unexpected results

expr is giving unexpected results for 4 characters (t, n, f, y). And if you are doing some further calculation. then code is breaking. I could not understand why this is happening?

% expr (F)
F

% expr (F)*1
can't use non-numeric string as operand of "*"

And,

% expr (t)
t
% expr (n)
n
% expr (f)
f
% expr (y)
y

This is coming file for charcters : t, n, f, y. There are no variables named by these characters. It should flag variable not found or some other valid error. Am i missing some thing?

Upvotes: 0

Views: 208

Answers (2)

keltar
keltar

Reputation: 18409

The [expr] conditions of commands such as [if] and [while] expect the expression to evaluate to a boolean, i.e., an integer or one of the following string values:

    true, on, yes
    false, off, no

I believe t, y, f and n are shortcuts for these.

Upvotes: 5

Marco Pallante
Marco Pallante

Reputation: 4043

I think you are expecting something wrong from expr.

That command is intended for evaluating expressions. It can do arithmetical operations on number, compare strings or number, execute some mathematical functions, and such.

Your lines

% expr (F)
% expr (t)
% expr (n)
% expr (f)
% expr (y)

all do the same thing: they ask to perform no operation on a literal string with higher precedence (the braces). So? There is nothing more and expr returns the string itself.

In

% expr (F)*1

however, you are trying to multiply a string to a number: an operation which is not defined. Indeed, expr gives you an error saying that one of the operands of * is a non numeric string (which number F should represent?).

With a literal string such F, or y, you can ask string comparison. For example, you can do these:

% expr F < f
1

(because in my encoding the upper case letters come before lower case ones)

% expr F == y
0

and so on.

So, expr is not giving any unexpected result, but maybe your expectations are wrong.

Upvotes: -1

Related Questions