Reputation: 25
In following code in TCL I cannot seem evaluate the variable "a"
I'm evaluating x and y, in the same For Loop I have a IF statement that is checking for a range between x and y.
If valid then I'd like to perform some more calculations within the IF condition.
Every thing is fine up to the IF condition, but I cant seem to evaluate "a".
I'm trying to set "a" to the value of "y" for all the values within the range $min <= $x && $x <= $max
I would kindly request the experts to highlight the mistake.
for {set i 0} {$i < $lenght} {incr i} {
set x [expr ([lindex $cal1 $i])*$offset]
set y [expr ((cal2)/2) ]
if {$min <= $x && $x <= $max } {
puts "is Active"
set a [lindex $y $i]
puts a = $a
}
}
Upvotes: 0
Views: 285
Reputation: 13252
There is a lot that seems problematic in your code.
In the first line, you use the variable lenght
. Tcl doesn't care about spelling, but if you don't have such a variable (you might possibly have a length
variable instead) you will get an error.
In the invocation expr ([lindex $cal1 $i])*$offset]
you have an unnecessary parenthesis but no braces around the expression (the braces aren't mandatory but should be there unless there is a very good reason to omit them). Also: "offset" usually means something you add to, not multiply with, another value. The invocation expr {[lindex $cal1 $i] * $offset}]
would be better.
The variable y
is used as a list argument to lindex
later on, but it's created as a scalar variable. Also, your expression divides a string (or rather, an invalid bareword) with 2. Maybe you meant lappend y [expr {$cal2 / 2}]
? If you use lappend
, each value will be added to the end of an existing list, or as the first element of a new list if y
doesn't exist. This is usually what one wants, but it means that the list y
should be reset using set y [list]
or set y {}
before entering the loop, to get rid of elements added earlier, if any.
puts a = $a
won't work, because if there are more than one argument to puts
they are expected to be the flag -nonewline
and/or a channel id to send the output to. Maybe you meant puts "a = $a"
.
Upvotes: 1