I have a "flat" Tcl list. Now I want to append a new element as a child to one of the existing elements. How can I do this?
This is what I tried:
[ lindex $flights $i ] [ lindex $flight 0 ] ]
I try to add an element form the list "flight" to an element of the list "flights". The element $i in the flights list already exists.
I might be running against Tcl syntax as I'm new to Tcl.
Thanks for your help.
You can use lset to replace an element of your list with a new list. http://www.tcl.tk/man/tcl8.5/TclCmd/lset.htm The first element of the new list will be the old element, the 2nd element will be its child. Here's an example:
% set flights [list a b c d e]
a b c d e
% set i 1
1
% lset flights $i [list b child]
a {b child} c d e
% lindex $flights 1
b child
% lindex [lindex $flights 1] 1
child
% lindex [lindex $flights 1] 0
b
Related
I just started learning Tcl, my main objective to print the two lists like this:
List1 List2
1 A
2 B
3 C
4 D
You can use a loop and format:
set List1 {1 2 3 4}
set List2 {A B C D}
puts [format %-10s%-10s List1 List2]
foreach i $List1 j $List2 {
puts [format %-10s%-10s $i $j]
}
Output from the above:
List1 List2
1 A
2 B
3 C
4 D
Note: In the above, %-10s roughly means left align the text within a column of 10 spaces
I have two lists
set a1 {a b c d e f}
set b1 {b f e}
I am trying to do remove_from_list $a1 $b1 >> {a c d}
Is there a function that can operate on lists on tcl?
To begin with, you can't use brackets for list literals. Brackets are used for command substitution in Tcl.
Instead, use the list-making command, list
% set a1 [list a b c d e f]
a b c d e f
Or, more or less equivalently:
% set b1 {b f e}
b f e
There is no standard command to subtract one list from another. It's very easy to construct a filter, though.
You can filter out items in a1 that are in b1 by an lmap (list map) filter:
lmap item $a1 {
if {$item ni $b1} {
set item
} else {
continue
}
}
# results in
a c d
The if command inside the lmap body determines if an item is not in (ni)$b1. If this is true, the value of the item becomes a part of the result. If it is false, the item is skipped.
In Tcl 8.5 and earlier, there is no lmap. In that case, one can copy-and-paste an ersatz lmap (works the same) into the code: link below.
Or, one can use foreach instead. It's a little messier but works.
set res {}
foreach item $a1 {
if {$item ni $b1} {
lappend res $item
}
}
% set res
a c d
Documentation:
continue,
foreach,
if,
lappend,
list,
lmap (for Tcl 8.5),
lmap,
ni (operator),
set
You can also use an array. It's easy to add and remove elements
% foreach elem $a1 {set x($elem) 1}
% foreach elem $b1 {unset x($elem)}
% set result [array names x]
d a c
It's a pretty efficient approach too, only a single pass through each list.
Or use a dictionary to maintain the original insertion order:
% foreach elem $a1 {dict set y $elem 1}
% foreach elem $b1 {dict unset y $elem}
% set result [dict keys $y]
a c d
# with struct::set from tcllib
package require struct::set
set a1 {a b c d e f}
set b1 {b f e}
struct::set difference $a1 $b1
# result in
d a c
Dokumentation:
struct::set
I'm familiar with finding the intersection of two lists, however, I wanted to find the union of two lists in tcl (while eliminating duplicates). I do have a working copy of this code, but I'm not sure it is robust enough for any kind/number of lists and am hence looking for a better solution.
Any help or ideas are appreciated.
If you treat lists as sets, so you don't worry about order if the items, you could just sort the joined list:
set union [lsort -unique [list {*}$list1 {*}$list2]]
Tclx provides a union command:
% info patchlevel
8.5.9
% set a [list a b c]
a b c
% set b [list a d e]
a d e
% package require Tclx
8.4
% union $a $b
a b c d e
%
% union
wrong # args: should be "union lista listb"
%
One way that doesn't need sorting is to use dictionary keys as sets:
% set a [list a b c]
a b c
% set b [list a d e]
a d e
% set d {}
% foreach k $a { dict set d $k . }
% foreach k $b { dict set d $k . }
% set c [dict keys $d]
a b c d e
This has the advantage of not needing to sort at all, which can help quite a lot with large input sets.
See code below:
set k [list]
foreach i [list 1 2] {
lappend k [ list "hey" [ list "ho" [ list $i ] ] ]
}
puts [ join $k ",and,"]
exit
The result is:
hey {ho 1},and,hey {ho 2}
But I expected the result to look like:
hey {ho {1}},and,hey {ho {2}}
Any ideas why is that so?
Thanks.
If anyone of the list command's arguments are more than elements one, then only that corresponding indexed element's return value will have the braced list form.
% list a b c; # All list args are having only single element
a b c
% list "a" "b" "c"; # Same as above
a b c
% list {a} {b} {c}; # Same again...
a b c
% list "a b" c d; # Here, 1st arg is having 2 elements.
{a b} c d
%
Tcl's wiki page already mentioned about bizarre behavior of the nested lists in only one case, which is
% list [list [list x]]
x
It means that Tcl lists alone cannot be used to represent ALL kinds of data structures, as Tcl lists magically collapse when it's a series of nested lists with the terminal list having only a single bare word that requires no escaping.
Update :
More importantly, if the arg is having a space in it,
% list "x "
{x }
% list "x"
x
%
Since the space has to be considered as well, Tcl has no other way, but to enclose the braces.
How te remove an element from TCL list say:
which has index = 4
which has value = "aa"
I have Googled and have not found any built-in function yet.
set mylist {a b c}
puts $mylist
a b c
Remove by index
set mylist [lreplace $mylist 2 2]
puts $mylist
a b
Remove by value
set idx [lsearch $mylist "b"]
set mylist [lreplace $mylist $idx $idx]
puts $mylist
a
The other way to remove an element is to filter it out. This Tcl 8.5 technique differs from the lsearch&lreplace method mentioned elsewhere in that it removes all of a given element from the list.
set stripped [lsearch -inline -all -not -exact $inputList $elemToRemove]
What it doesn't do is search through nested lists. That's a consequence of Tcl not putting effort into understanding your data structures too deeply. (You can tell it to search by comparing specific elements of the sublists though, via the -index option.)
Lets say you want to replace element "b":
% set L {a b c d}
a b c d
You replace the first element 1 and last element 1 by nothing:
% lreplace $L 1 1
a c d
regsub may also be suitable to remove a value from a list.
set mylist {a b c}
puts $mylist
a b c
regsub b $mylist "" mylist
puts $mylist
a c
llength $mylist
2
Just wrapped up what others have done
proc _lremove {listName val {byval false}} {
upvar $listName list
if {$byval} {
set list [lsearch -all -inline -not $list $val]
} else {
set list [lreplace $list $val $val]
}
return $list
}
Then call with
Inline edit, list lappend
set output [list 1 2 3 20]
_lremove output 0
echo $output
>> 2 3 20
Set output like lreplace/lsearch
set output [list 1 2 3 20]
echo [_lremove output 0]
>> 2 3 20
Remove by value
set output [list 1 2 3 20]
echo [_lremove output 3 true]
>> 1 2 20
Remove by value with wildcar
set output [list 1 2 3 20]
echo [_lremove output "2*" true]
>> 1 3
You can also try like this :
set i 0
set myl [list a b c d e f]
foreach el $myl {
if {$el in {a b e f}} {
set myl [lreplace $myl $i $i]
} else {
incr i
}
}
set myl
There are 2 easy ways.
# index
set mylist "a c b"
set mylist [lreplace $mylist 2 2]
puts $mylist
a b
# value
set idx [lsearch $mylist "b"]
set mylist [lreplace $mylist $idx $idx]
puts $mylist
a