Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 : ""]
like image 481
MikeKulls Avatar asked Nov 20 '25 06:11

MikeKulls


1 Answers

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.

like image 58
Peter Lewerin Avatar answered Nov 24 '25 09:11

Peter Lewerin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!