Given:
my @mylist1; push(@mylist1,'A'); push(@mylist1,'B'); push(@mylist1,'C'); my @mylist2; push(@mylist2,'A'); push(@mylist2,'D'); push(@mylist2,'E');
What’s the quickest way in Perl to insert in mylist2 all elements that are in mylist1 and not already in mylist2 (ABCDE).
Alternatively:
Actually – these might be wrong because they don’t account for whether duplicates might exist in either of the original lists.
You didn’t say in your question whether the lists are supposed to represent sets (which can’t contain duplicates) or just plain lists. That you effectively want
@mylist2 = @mylist1 U @mylist2suggests that you are treating them as sets.EDIT: changed increment to assign – saves a read of the hash value