in reply to Gift Exchange Code

As MidLifeXis mentions above, this seems to be a very easy problem if you only need one working match. Is there some further requirement? Do you maybe want to see lots of possibilities, and pick only the best one? If you really just need one match, then let's suppose that you have an array @people of people, and a hash %so_s with the properties that keys %so_s consists only of people with SOs and that, whenever $a and $b are SOs, exactly one of $a or $b is a key of %so_s, with the other as the corresponding value. (In your example, %so_s could be ( a => 'b' ) or ( b => 'a' ).) Then
die "No match possible" if @people % 2 or @people == 2 and %so_s; %reverse_so_s = reverse %so_s; @keys = keys %so_s; @values = values %so_s; @singles = grep { ! ( exists $so_s{$_} or exists $reverse_so_s{$_} ) } + @people; %matches = ( @keys, @singles, @values );
should do it.

UPDATE: Oops, CountZero posted a very similar solution already. Much of my set-up is designed just to construct his or her @array1 and @array2.

UPDATE 2: Oops again. This dies far too often. (For example, it thinks that no match is possible if a, b, and c are affianced to d, e, and f, respectively, whereas that is not the case.) A few minutes' thought didn't show how to fix this without serious re-jiggering.

UPDATE 3: Third time's a charm, I hope. (Hey, maybe this is why people use modules instead of worrying about edge cases in their own code, huh?)