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?)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.