I want to remove a sublist from a list in Tcl. I know how to do it for main list using lreplace
but I don't know how to do it for a sublist.
For example:
set a { 1 2 { {3 4} { 4 } } }
Now I want to remove {4}
from internal list { {3 4} {4} }
.
The final list should be:
a { 1 2 { {3 4} } }
Please suggest how to dot his.
Combine lindex
to get the internal sublist, lreplace
to delete an element of the extracted internal sublist and lset
to put the modified sublist back in place.
But honestly I have a feeling something's wrong about your data model.
proc retlist {a} {
set nl ""
foreach i $a {
if {[llength $i] > 1} {
set nl2 ""
foreach i2 $i {
if {[llength $i2] > 1} { lappend nl2 $i2 }
}
lappend nl $nl2
} else {
lappend nl $i
}
}
return $nl
}
If you need variable depth you're lost with this code. You need recursion for that.
% set a {1 2 {{3 4} {5}}}
1 2 {{3 4} {5}}
% retlist $a
1 2 {{3 4}}
As the outer list is never displayed in tclsh.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With