0

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

}

}

1 Answer 1

1

There is a lot that seems problematic in your code.

  1. 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.

  2. 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.

  3. 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.

  4. 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".

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

1 Comment

Hi Peter, Thank you for your feedback. Firstly apologies, I had to copy a snippet from a larger chuck of code. Therefore ended up making a few errors even when copying! I am using offset as you mentioned but also reusing it as a gain.

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.