MikeKulls
MikeKulls

Reputation: 1006

TCL ternary operator does not like empty string

Simple question, TCL gives me "premature end of expression" because the last param of the ternary expression is an empty string. If it is anything else then it works ok. Can I use an empty string here?

set y 5
set x [expr ($y > 1) ? 1 : ""]

Upvotes: 3

Views: 2842

Answers (1)

Peter Lewerin
Peter Lewerin

Reputation: 13252

You should always give expr a single, complete expression, wrapped in braces, as argument. This avoids a whole slew of problems, including this one.

expr {$y > 1 ? 1 : ""}

The problem is that expr concatenates its arguments to get an expression. The invocation concat $y > 1 ? 1 : "" gives the string "5 > 1 ? 1 : ", which can't be parsed by expr.

Upvotes: 10

Related Questions