for {set i 0} {$i < 5} {incr i} {
puts "I inside first loop: $i"
}
Is it possible to increment i by .5 instead of 1 ?
Now the above code is providing below output:
i inside first loop: 0
i inside first loop: 1
i inside first loop: 2
i inside first loop: 3
i inside first loop: 4
but I need something like this:
I inside first loop: 0
I inside first loop: .5
I inside first loop: 1
... so on
Tcl's incr command only handles integer values. The recommended way of getting a loop value that steps by some fractional value is to use a integer loop counter and then compute the fractional value from it:
for {set i_int 0} {$i_int < 5} {incr i_int} {
set i [expr {$i_int * 0.5}]
puts "I inside first loop: $i"
}
This is important when the fractional step is not a simple multiple of a power of two; while 0.5 can be represented exactly in binary floating point arithmetic (it's 2-1 after all) 0.1 can't (just as 1/3 can't be written exactly in a finite number of decimal places).
By default, incr increments by 1 unit when no specific number is mentioned.
incr i 2
will increment i by 2 on each iteration.
incr i -1
will decrement i by 1 on each iteration.
You can thus change the number to be whatever you need it to be.
The only problem is that you can only increment by an integer. So you'll have to use something else for the 0.5. You can use expr perhaps?
for {set i 0} {$i < 5} {set i [expr {$i+0.5}]} {
puts "I inside first loop: $i"
}
EDIT: Actually, Donal's answer is better since it doesn't have the rounding errors :)
for {set i 0} {$i < 5} {incr i} {
puts "I inside first loop: [expr $i*0.5]"
}
Related
Could someone help me to understand why the below code hangs (i.e. stuck in an endless loop) in Xcelium simulator, and what should be the correct one?
for {set i 0} {$i < 2} {incr $i} { puts "i is $i"; }
set i 0
while {$i < 2} { puts "i is $i"; incr $1; }
The result for either for or while loop above is just an endless: i is 0
incr takes a variable name as input argument, so it should be incr i.
incr $i increments instead the variable 0. What happens if the variable does not exist depends on Tcl version. From the manual page:
Starting with the Tcl 8.5 release, the variable varName passed to incr may be unset, and in that case, it will be set to the value increment or to the default increment value of 1.
Sorry, that's a very trivial question, but it seems to be so trivial that I can't find any recommendation: What is the best way in Tcl to test whether a list is empty? I would go for if {$myList eq ""} ..., or is there a better way? Thanks for your help!
If you know you have a list (because you've made it that way or are using other operations that treat the value as such), the best empty check is looking at the llength of it to see if it is zero. I usually prefer to use the result as if it was boolean, like this, but comparing to zero also works just fine.
if {[llength $theList]} {
puts "the list is NOT empty"
} else {
puts "the list IS empty"
}
Yes, this will incur the cost of converting the value to a list, but that conversion is cached within the value itself (this is a type of “internal representation”) so the next operation to use it as a list is then much faster as it doesn't have to reparse anything.
I just want to share some test code which shows that if {$myList eq ""}... can be much slower than if {![llength $myList}} ...:
set repeats 10
foreach n {10 100 1000 10000 20000} {
puts "n = $n"
puts [time {
set l [list]
for {set i 0} {$i < $n} {incr i} {
lappend l $i
}
} $repeats]
puts [time {
set l [list]
for {set i 0} {$i < $n} {incr i} {
lappend l $i
if {![llength $l]} {
puts "empty (llength)"
}
}
} $repeats]
puts [time {
set l [list]
for {set i 0} {$i < $n} {incr i} {
lappend l $i
if {$l eq ""} {
puts "empty (eq)"
}
}
} $repeats]
}
Here is the output; for each value of n: baseline (no check), check with llength, check with eq:
n = 10
5.0 microseconds per iteration
14.1 microseconds per iteration
14.0 microseconds per iteration
n = 100
40.6 microseconds per iteration
44.0 microseconds per iteration
105.8 microseconds per iteration
n = 1000
374.4 microseconds per iteration
440.7 microseconds per iteration
6240.1 microseconds per iteration
n = 10000
3534.4 microseconds per iteration
4485.8 microseconds per iteration
667206.0 microseconds per iteration
n = 20000
7576.7 microseconds per iteration
9051.9 microseconds per iteration
2761285.3 microseconds per iteration
I would guess that the solution using if {$l eq ""}... is very slow since does a conversion from a list to a string every time the check is done. Or is there a different explanation?
I need to make a string of n 1's in tcl where n is some variable, how can I do this nicely?
At the moment I'm doing this but there must be a better way.
set i 1
set ones 1
while {$i < $n} {
set ones 1$ones
incr i}
In python I'd write "1"*n.
Solution 1:[Simple Solution]
set n 10
puts "[string repeat "1" $n]" ;# To display output on console
set output_str [string repeat "1" $n] ;# To get output in variable
Solution 2:
You have to append "one" inside string n number of times, where n is number of ones you want in string.
set n 10
set i 0
set ones 1
set output_str ""
while {$i < $n} {
append output_str $ones
incr i
}
Output,
puts $output_str ;#Gives output 1111111111
There is a built in string command to do this:
% set n 10
10
% string repeat "1" $n
1111111111
%
I want to implement the following C code in TCL:
Float power_pitch = 8
float offset = 7.5
float threshold = 100
for(i=power_pitch+offset ; i<= threshold ; i += power_pitch)
I want to implement above forloop in TCL. I have tried the following code in TCL:
set power_pitch 8
set offset 7.5
set threshold 100
for { set i [expr $power_pitch + $offset] } { $i <= $threshold } { incr i $power_pitch}
but when I execute above code I get the following error:
expected integer but got "15.5 "
while executing incr i $power_pitch
Could you help me to implement above forloop in TCL?
The incr command only works with integers. Otherwise, use:
set i [expr {$i + $power_pitch}]
The for command itself won't mind. (Be aware of float rounding issues; they're not Tcl-specific, but can hit with anything that isn't an integer multiple of a power of 2…)
Donal has already provided the answer to the question, I'd just like to make an observation in two points about the for command.
for is very nearly free-form
while an integral counting loop is a typical use of for, it's by no means the only option
The for command has the synopsis
for start test next body
where start, next, and body are command strings (i.e. appropriate as an argument to eval; they can be empty, contain single commands, or be full scripts) and test is a boolean expression string (i.e. appropriate as an argument to expr and evaluating to something that is or can be coerced into a boolean value).
Usually, start is used to set up for test and body, and next is supposed to bring the state incrementally closer to having test return a false value, but that's just a convention, not a requirement. The following are perfectly valid (but rather smelly) invocations:
for {set n 0 ; puts -nonewline X} {[incr n] < 5} {puts -nonewline X} {
puts -nonewline [string repeat - $n]
}
for {set f [open foo.txt] ; set n 0} {$n >= 0} {puts $line} {
set n [chan gets $f line]
}
Give for any combination of command strings and boolean expression, and it will run. It might execute its body forever or not even once, but it will run. Don't limit yourself to for {set i 0} {$i < 10} {incr i} {...} invocations, that's 1950s thinking.
Even if you just want to use it for counting loops, there are still lots of options, for instance:
for {set i 0} {$i < $limit} {incr i} {...} ;# simple increment
for {set i 0} {$i < $limit} {incr i $n} {...} ;# stepping increment/decrement
for {set i 0} {$i < $limit} {incr i $i} {...} ;# doubling increment
for {set i 0} {$i < $limit} {set i [expr {...}]} {...} ;# arbitrary change
Free your mind, the rest will follow.
Documentation: chan, expr, for, incr, open, puts, set, string
I try to write a very simple program in TCL using list.
Below is the list
list { 1 2 3 4 5 6 1.5 7 }
Below is my code
set sum 0
for {set i 0} {$i < [llength $list]} {incr i} {
incr sum [lindex $list $i]
}
puts $sum
On executing the above program I am getting the below error due to floating point value of 1.5 in the list
expected integer but got "1.5"
(reading increment)
invoked from within
"incr sum [lindex $list $i]"
I searched on internet and could not find anything relevant.
Please advise how do I handle the floating point value?
While using incr command, variable must have value that can be interpreted as a an integer. See tcl wiki.
If variable is a non-integral real number, [incr] could not be used, but [set] could:
set sum 0
for {set i 0} {$i < [llength $list]} {incr i} {
set sum [expr {$sum + [lindex $list $i]}]
}
puts $sum
Omsai's answer should solve your problem, but a cleaner solution is to use foreach:
set sum 0
foreach n $list {
set sum [expr {$sum + $n}]
}
puts $sum
Summing up a list of numeric values can also be done with the ::tcl::mathop::+ command:
::tcl::mathop::+ {*}$list
This looks more complicated that it is. The + command isn't available in the regular namespace, so you need to specify where it comes from (the ::tcl::mathop namespace). The command expects to get each operand as a separate argument, so if they are in a list you need to expand that list using the {*} prefix.
foreach and the various mathop commands are documented here: foreach, mathop.
(Note: the 'Hoodiecrow' mentioned in the comments is me, I used that nick earlier.)
Tcl gives an error if you will try
incr a 1.5
you have to change the logic.
clearly you want to add all the numbers in the list. and answers are easy and many. But i will give you the shortest way:
set l { 1 2 3 4 5 6 1.5 7 }
set sum [expr [join $l +]]
NO LOOPING REQUIRED.