Suppose I capture the number of files in a directory using glob
and llength
:
# For this example, suppose llength returned 4.
set number_of_images [llength [glob -nocomplain -directory $destination_folder -type f *]]
I would like to use that number and increment it in a foreach
loop depending on the number of files in another directory.
foreach file [glob -nocomplain -directory $source_folder -type f *] {
puts [incr $number_of_images]
# I want to start the count at 5, and increment by 1 with each loop. incr fails to
# do so, as well as mathfunc::int.
}
My issue is, according to the documentation, llength
returns a string:
Treats list as a list and returns a decimal string giving the number of elements in it.
Can I convert it to an int
and use it?
In Tcl, everything is a string. Yes you can increment a variable that holds an integer (represented as a decimal string).
The issue is with your use of incr
. The incr
command is used to increment a variable holding an integer, not an integer value. Simply change your code from:
puts [incr $number_of_images]
... to ...
puts [incr number_of_images]