admin管理员组

文章数量:1332383

I have this list

set csv [list]
lappend csv [list \
a \
b \
c \
[expr {[cfg::get enable_all_columns]
? {
d
}
: {}
}]  \
e \
f \
g]

I need to add element d only if enable_all_columns is 1. The problem is that with this approach if it is 0, it returns something empty which creates a new column in csv raport.

That lappend to output this list {a b c d e f g} I tried to break the lappend to put the condition and then add the last ones, but this results in this {a b c} d e f g I also tried linsert but it did the same

set csv [list]

lappend csv [list \
a       \
b]

set csv [linsert $csv 1 x]

puts "$csv"

I have this list

set csv [list]
lappend csv [list \
a \
b \
c \
[expr {[cfg::get enable_all_columns]
? {
d
}
: {}
}]  \
e \
f \
g]

I need to add element d only if enable_all_columns is 1. The problem is that with this approach if it is 0, it returns something empty which creates a new column in csv raport.

That lappend to output this list {a b c d e f g} I tried to break the lappend to put the condition and then add the last ones, but this results in this {a b c} d e f g I also tried linsert but it did the same

set csv [list]

lappend csv [list \
a       \
b]

set csv [linsert $csv 1 x]

puts "$csv"
Share edited Nov 22, 2024 at 9:18 Schelte Bron 4,8131 gold badge8 silver badges13 bronze badges asked Nov 22, 2024 at 5:10 RazvanRazvan 11 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 0

I would do it this way:

set csv {a b c e f g}
if {[cfg::get enable_all_columns]} {
    set csv [linsert $csv 3 d]
}

Expansion allows you to make something that disappears when empty.

set csv [list]
lappend csv [list a b c {*}[if {[cfg::get enable_all_columns]} {
    list d
}] e f g]

本文标签: insertTCLA condition for an item while appending items to a listStack Overflow