Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Given that it seemed like an intresting problem I opted for re-inventing the wheel and not using Graph. See the readmore for the code.

Do note that the best solution is to use Graph since it went trough more testing and might be optimized. The code in the readmore is only provided as a learning-exercice on how to do it yourself.

#!/usr/bin/perl -l use strict; use warnings; no warnings qw/uninitialized/; for ( [ "1,7", "2,6", "2,7", "5,4", "6,7" ], [ "1,7", "2,6", "2,7", "3,4", "6,7" ], [ "1,7", "2,6", "2,7", "3,4", "9,8" ], [ "a,b", "c,b", "d,f", "e,b", "f,g" ], ) { my @pairs = @$_; my $count = 0; my %sets; my %sets2; for my $p (@pairs) { my ($l, $r) = split /\s*,\s*/, $p; # print "P = $p, L = $l, R = $r, \$sets{\$l} = $sets{$l}, \$sets{\$ +r} = $sets{$r}"; if ($sets{$l} and $sets{$r} and $sets{$l} != $sets{$r}) { # $sets{$l} and $sets{$r} can now be linked my $old_set = $sets{$r}; for my $s (keys %sets) { if ($sets{$s} == $old_set) { if (exists $sets2{ $sets{$s} }) { push @{ $sets2{ $sets{$l} } }, @{ $sets2{ $sets{$s} } }; delete $sets2{ $old_set }; } $sets{$s} = $sets{$l}; } } } elsif ($sets{$l}) { $sets{$r} = $sets{$l}; } elsif ($sets{$r}) { $sets{$l} = $sets{$r}; } else { $count++; $sets{$l} = $sets{$r} = $count; } push @{ $sets2{ $sets{$l} } }, $p; } print "SETS FOR: " . join(", ", @pairs); my $i = 0; for my $key (keys %sets2) { print "\tSET $i: " . join(", ", @{ $sets2{ $key} }); $i++; } print ""; } __END__ Output: SETS FOR: 1,7, 2,6, 2,7, 5,4, 6,7 SET 0: 5,4 SET 1: 2,6, 1,7, 2,7, 6,7 SETS FOR: 1,7, 2,6, 2,7, 3,4, 6,7 SET 0: 3,4 SET 1: 2,6, 1,7, 2,7, 6,7 SETS FOR: 1,7, 2,6, 2,7, 3,4, 9,8 SET 0: 9,8 SET 1: 3,4 SET 2: 2,6, 1,7, 2,7 SETS FOR: a,b, c,b, d,f, e,b, f,g SET 0: a,b, c,b, e,b SET 1: d,f, f,g

In reply to Re: results from pairs combination by Animator
in thread results from pairs combination by lipong

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-25 10:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found