fork download
  1. #!/usr/bin/perl
  2.  
  3. %h;
  4. while(<>){
  5. ($f, $s) = split/ /;#first, second
  6.  
  7. undef $keyf;
  8. undef $keys;
  9.  
  10. for $k (keys %h){
  11. $keyf = $k if grep /$f/, @{$h{$k}};
  12. $keys = $k if grep /$s/, @{$h{$k}};
  13. }
  14.  
  15. if(!defined $keyf and !defined $keys){
  16. push @{$h{$.}}, $f;
  17. push @{$h{$.}}, $s;
  18. }else{
  19. push @{$h{$keys}}, $f unless defined $keyf;
  20. push @{$h{$keyf}}, $s unless defined $keys;
  21. }
  22. }
  23.  
  24. map{print join " ", (sort{$a cmp $b}@{$h{$_}}), "\n"}keys %h;
Success #stdin #stdout 0s 17496KB
stdin
A B
C D
A C
stdout
A B 
C D