Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCL use "eq" or "==" when the argument can be a list or a string

I have the following if loop:

if { expr { $Node_3 eq "BadAngle" } } {
  return "BadAngle"
}

Node_3 can be a list containing coordinates, or the simple string "BadAngle" I use TCLPro 1.4 for debugging and TCL 8.6. I get:

*syntax error in expression " expr { $Node_3 eq "BadAngle" } "*

I then also tried:

if { [ expr { $Node_3 eq "BadAngle" ] == 1 } } {
   return "BadAngle"
}

But i get the same error. Also: What is the better alternative in this case: To use "==" or "eq", I think "eq" because a list is a kind of a string..or?

like image 991
Lumpi Avatar asked Jul 17 '13 09:07

Lumpi


Video Answer


1 Answers

You seem to be getting in a bit of a pickle there. Firstly, you probably don't want to use expr inside the if condition, so this will be enough:

if { $Node_3 eq "BadAngle" } {
    return "BadAngle"
}

Since one of the things you are comparing against is definitely non-numeric, you'll be fine using the eq operator though the == operator is equivalent. The only time there is a difference is when both operands look like numbers, when == will compare them as numbers (parsing them into numbers if necessary). This means that "1" == "0x01" despite them being rather different strings; the eq operator always compares as strings.

How to choose which is best? It's actually pretty easy: do you think you are working with numbers at this point on both sides of the comparison? If yes, use ==, and otherwise use eq.


The one time you want to use expr inside if is when you're dynamically selecting the operator. This is not really recommended, but you'd do it like this:

set op "=="
if { [expr {$Node_3} $op {"BadAngle"} ]} {
    return "BadAngle"
}

It's pretty ugly. (Notice that I put everything else inside its own braces to prevent double evaluation, and I'm careful with balancing all the brackets correctly, which is what was tripping you up in the code in your question.) Don't do it unless you really really need to.

like image 158
Donal Fellows Avatar answered Sep 19 '22 11:09

Donal Fellows