1

1)

% expr "1==1"
1

2)

% expr "i==i"
invalid bareword "i"
in expression "i==i";
should be "$i" or "{i}" or "i(...)" or ...

Why am getting this error in step - 2

1) % if {"i" == "i"} {
    puts "hai"
  }
  hai



2) % if {i == "i"} {
   puts "hai"
  }

invalid bareword "i"
in expression "i == "i"";
should be "$i" or "{i}" or "i(...)" or ...

if {"i" == "i"} This is wotking with if condition .

Here I found like expr command evaluating only integers , not comparing strings , But the In "if" condition everything (integer as well as string) are evaluating .

How Things are working here ?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
velpandian
  • 431
  • 4
  • 11
  • 23

2 Answers2

9

The answer is in the expr man page.

Operands may be specified in any of the following ways:
...
[4]    As a string enclosed in double-quotes.   The  expression  parser
       will  perform  backslash, variable, and command substitutions on
       the information between the quotes, and use the resulting  value
       as the operand

[5]    As a string enclosed in braces.  The characters between the open
       brace and matching close brace will be used as the operand with‐
       out any substitutions.
...

So, expr can compare strings, but you must enclose them in double quotes or curly braces, depending on whether you want substituions performed or not.

Therefore, in your example 2, you must use

% expr {"i" == "i"}

or

% expr {{i} == {i}}

Better to user the string comparison operands:

% expr {"i" eq "i"}
% expr {{i} eq {i}}

to be sure that the content of the string is not converted to numerical values.

Marco Pallante
  • 3,923
  • 1
  • 21
  • 26
  • 1
    Even I tried the same but stil am getting same error . % expr "i" == "i" invalid bareword "i" in expression "i == i"; should be "$i" or "{i}" or "i(...)" or ... % – velpandian Sep 16 '13 at 11:07
  • 1
    @user2746819 You can do `expr {"i" == "i"}` – Jerry Sep 16 '13 at 11:09
  • @Jerry is right: I missed the enclosing `{...}`. I'm editing the answer – Marco Pallante Sep 16 '13 at 11:10
  • @Jerry : It's working . Can you explain me what is going on in this expression compared to previous one ? – velpandian Sep 16 '13 at 11:14
  • @user2746819 Well, I cannot say I'm 100% sure, but how it works is a bit similar to `if`, because you use `if {"i" == "i"}`. Wrapping the `i` in quotes is telling Tcl that this is text and hence compares them as such. I would be using `string compare` though, for string comparison or `if {"i" eq "i"}` for if. I use `expr` only for calculations. – Jerry Sep 16 '13 at 11:18
  • 2
    @Jerry, it's the other way round ;-) `if` and other conditional commands use the same engine `expr` does. – kostix Sep 16 '13 at 11:25
  • @user2746819 `if` calls `expr` on its first argument, so they work the same. You need to double quote (or wrap in curly braces) the string because this is how the `expr` parser wants it. You also need to escape the double quotes so that they are not interpreted by the main parser, but passed to the `expr` parser. Another way to do that, but it's ugly, is `expr \"i\" eq \"i\"` – Marco Pallante Sep 16 '13 at 11:26
  • @user2746819, be sure to read and understand [this thread](http://stackoverflow.com/q/17451671/720999) to make yourself accustomed to how `expr` works. Also working yourself through [the tutorial](http://www.tcl.tk/man/tcl8.5/tutorial/tcltutorial.html) is a must. – kostix Sep 16 '13 at 11:27
  • 2
    @Marco That's true, except it's more accurate to say that both `if` and `expr` call the same underlying engine. (As do `while` and `for`.) – Donal Fellows Sep 16 '13 at 12:29
0

In Tcl 8.4

you can use

  %expr {"i" == "i"}

or

  %expr ( "i" == "i" )

Both syntaxes will work.

Daniel
  • 20,420
  • 10
  • 92
  • 149
user2693933
  • 70
  • 2
  • 7
  • Only the first here is better one. The recommended is `expr {"i" eq "i"}`. The second one will be [slow](http://wiki.tcl.tk/10225). – Jerry Oct 18 '17 at 12:20