dimanche 12 septembre 2021

conceptual error with control flow in tcl

I have a collection of things, some of them empty.

I want to form a collection of non empty things, separated by separator.

this is essentially what I do in c++, but fails with any and all combinations of $ signs etc. etc.

I've already got a work around thanks, I'd like to know why and how this fails.

set q1 "a"
set q2 ""
set q3 "c"
set q4 d
set q5 ""    

set answer ""

set needSeparator 0
foreach { var } { 
    q1 q2 q3 q4 q5 
    } {
    if { $var ne "" } {
        if {$needSeparator} {
            append answer " separator "
        }
        append answer $var
        set needSeparator 1
    }
}

# expecting answer to be "a separator c separator d"
puts $answer

Aucun commentaire:

Enregistrer un commentaire