0

TCL coding if statement + expr error

 if {[expr 2000.0 - [lindex $llend [expr $i+0] 0 ]] > 80 {
     puts "Error: Results > 80"
 }

The if statement is putting out incorrect results hence, I am getting "Error when < 80".

Output file: 2000 - 1953.0 Error: Results > 80

5
  • 1
    You're missing the closing brace for the if expression. If that's just a typo, then without knowing what llend and i are, it's impossible to say why the math is wrong. Commented Feb 14 at 13:19
  • 2
    Note that the first argument to if is already evaluated (internally) by expo, so you can write if {2000.0 - [lindex $llend $i 0] > 80} {...} Commented Feb 14 at 13:21
  • It's very rare to want to put expr inside an if test, or to not brace the expression for expr to evaluate. Are you putting complex expressions in either the llend or i variables? Commented Feb 15 at 2:54
  • Thanks Glenn. It works for 1 test case but not sure why another test case with the same command it errors out and say "OPERAND @ 2000_ issue"? Commented Feb 15 at 6:21
  • Ah! That tells me exactly what the problem is... Commented Feb 15 at 12:07

1 Answer 1

2

Your code (after syntax correction) is:

if {[expr 2000.0 - [lindex $llend [expr $i+0] 0 ]] > 80} {
    puts "Error: Results > 80"
}

That has the problem that it is using unbraced expressions, and that's almost always a mistake. In particular, when the lindex returns an empty string, it will produce an extremely misleading error.

If we rewrite in conventional form (bearing mind that the condition argument is an expression anyway):

if {2000.0 - [lindex $llend $i 0] > 80} {
    puts "Error: Results > 80"
}

Then the code (coincidently now efficiently bytecode-able) will probably produce a more informative error telling you that you can't use an empty string as an argument to subtract.

If, in the unlikely case you have a list of expressions rather than numbers, you need to evaluate an inner expression, it is still best to limit the scope of that and only ever provide a single argument to expr, like this:

if {2000.0 - [expr [lindex $llend $i 0]] > 80} {
    puts "Error: Results > 80"
}

There's literally never a good reason to apply [expr ...+0] to an index argument to lindex.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried the above suggestion. And it is giving me error message "empty expression in expression ""
Yes. Presumably because you are indexing off the end of some list. That isn't the fault of the code you posted but rather of what the variables have been set to. We will need more context to help with that.
I have a list {1 1} {2 2} {3 3} {} . And it seems that the issue is with the empty list {}. I put if {lindex $llx2 0] - [lindex $ll $i 0] > 100. It will error out because of {}. What would be the syntax or TCL to avoid the {} - empty string in the if statement?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.