Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the union of two lists in tcl?

Tags:

list

tcl

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.

like image 562
stark Avatar asked Jan 29 '26 02:01

stark


2 Answers

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]]
like image 52
Sergei Golovan Avatar answered Feb 02 '26 14:02

Sergei Golovan


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"
%
like image 23
Sharad Avatar answered Feb 02 '26 14:02

Sharad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!