expr eq/ne operator puzzle
Author Message
expr eq/ne operator puzzle

What is the difference between these expr commands?
Why do we need the braces in some, but not others?

% set a blue

% expr \$a eq "blue"
syntax error in expression "blue eq blue": variable references require preceding \$

% expr {\$a eq "blue"}
1

% expr "aaa" eq "aaa"
syntax error in expression "aaa eq aaa": variable references require preceding \$

% expr {"aaa" eq "aaa"}
1

% expr \$a eq \$a
syntax error in expression "blue eq blue": variable references require preceding \$

% expr {"aaa" == "aaa"}
1
% expr {\$a eq \$a}
1

% expr 1 eq 1
1
% expr 1 == 1
1
% expr {1 == 1}
1

Tue, 30 Sep 2008 13:39:31 GMT
expr eq/ne operator puzzle
[expr] tries to interpret all its arguments as numbers first. The ne
and eq operators are there to make it interpret the arguments as
strings.

When you do not use braces, the variables are expanded, quotes are
removed and other substitutions take place _before_ expr has a chance
to interpret them.

Regards,

Arjen

Tue, 30 Sep 2008 15:30:01 GMT
expr eq/ne operator puzzle

Quote:

> What is the difference between these expr commands?
> Why do we need the braces in some, but not others?
> % set a blue

> % expr \$a eq "blue"
> syntax error in expression "blue eq blue": variable references require preceding \$

The double quotes are removed by Tcl before the call, so expr sees
exactly what is says: blue eq blue. Expr, unlike Tcl, wants strings to
always be surrounded by double quotes or braces, to distinguish from
operators etc.

Quote:

> % expr {\$a eq "blue"}
> 1

The braces here prevent Tcl doing substitutions, so the double quotes
are passed on to expr. Expr sees: \$a eq "blue"

This is valid: variable operator string.

Quote:

> % expr "aaa" eq "aaa"
> syntax error in expression "aaa eq aaa": variable references require preceding \$

Again, the quotes are stripped before expr sees it.

Quote:

>  % expr {"aaa" eq "aaa"}
> 1

> % expr \$a eq \$a
> syntax error in expression "blue eq blue": variable references require preceding \$

Tcl substitutes the variables, expr sees: blue eq blue (as it says),
missing the required quotes.

Quote:
> % expr {\$a eq \$a}
> 1

Here, expr sees: \$a eq \$a, which parses as: variable operator variable.
Even though the value of \$a doesn't contain double quotes, expr is fine
with this, as it has already done the parsing by this point.

Quote:

> % expr 1 eq 1
> 1
> % expr 1 == 1
> 1
> % expr {1 == 1}
> 1

Numbers are unambiguous to expr.

Hope this helps. Basically, if you don't want to spend your time trying
to remember how expr parsing works, then it's best to just use braces
always.

-- Neil

Tue, 30 Sep 2008 15:40:58 GMT
expr eq/ne operator puzzle
[TIP]

Also remember that if you *know* your are dealing with strings only and
*not* numbers, you should use "eq" and "ne" as it speeds up the
operations because expr tries to do number first.

Robert

Tue, 30 Sep 2008 20:19:55 GMT
expr eq/ne operator puzzle

http://www.tclscripting.com/articles/jan06/article4.html

Robert

Tue, 30 Sep 2008 20:26:43 GMT

 Page 1 of 1 [ 5 post ]

Relevant Pages