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
I should also mention that I read about that here:

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

Robert



Tue, 30 Sep 2008 20:26:43 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. when did expr eq and ne appear?

2. __eq__ and __ne__

3. failure in: expr $a ne $b

4. ( 2.31.New operators: 'eq', 'ne', 'last', '..' ) ?

5. TIP #133: Extending [expr] Operators

6. TIP #123: Adding an Exponentiation Operator to the [expr] Command

7. tcl8.5 new operator in expr

8. expr operator precedence

9. Overloading of expr operators

10. Expr operators

11. X != [expr tan([expr atan(X)])] ????

12. operator overloading and user definable operators.

 

 
Powered by phpBB® Forum Software