Using Raku (formerly known as Perl_6)
Raku has Set
object types, and you can read individual files to create Sets from lines:
~$ raku -e 'my $a = Set.new: "list1".IO.lines;
my $b = Set.new: "list2".IO.lines;
say "list1 = ", $a;
say "list2 = ", $b;'
list1 = Set(a b c d)
list2 = Set(c d e f)
You can perform asymmetric Set differences, with either ASCII infix (-)
, or Unicode infix ∖
:
~$ raku -e 'my $a = Set.new: "list1".IO.lines;
my $b = Set.new: "list2".IO.lines;
say $a (-) $b;'
Set(a b)
~$ raku -e 'my $a = Set.new: "list1".IO.lines;
my $b = Set.new: "list2".IO.lines;
say $b (-) $a;'
Set(e f)
OTOH, sometimes you need to perform a symmetric Set difference, and Raku has you covered. Use either ASCII infix (^)
or Unicode infix ⊖
:
~$ raku -e 'my $a = Set.new: "list1".IO.lines;
my $b = Set.new: "list2".IO.lines;
say $a (^) $b;'
Set(a b e f)
Finally, you can get linewise output by changing the final line to .keys.put for
… .
Final symmetric Set difference example below, using Unicode infix ⊖
operator:
~$ raku -e 'my $a = Set.new: "list1".IO.lines;
my $b = Set.new: "list2".IO.lines;
.keys.put for $a ⊖ $b;'
f
e
a
b
https://docs.raku.org/type/Set
https://docs.raku.org/language/setbagmix#Operators_with_set_semantics
https://raku.org