#!/usr/bin/perl use strict; use warnings; chomp(my @lines = ); my @words = map [ split /_/ ], @lines; my %matches; my $comb = combinations( 0 .. $#words ); while (my @comb = $comb->()) { next unless @comb == 2; my ($i, $j) = @comb; for my $wi (@{$words[$i]}) { for my $wj (@{$words[$j]}) { $matches{"$i.$j"}++ if $wi eq $wj; } } } for (grep $matches{$_} > 1, keys %matches) { my ($i, $j) = split /\./; print "$lines[$i] and $lines[$j]\n"; } # from 197008 sub combinations { my @list = @_; my @pick = (0) x @list; return sub { my $i = 0; while (1 < ++$pick[$i]) { $pick[$i] = 0; return if $#pick < ++$i; } return @list[ grep $pick[$_], 0..$#pick ]; }; } __DATA__ one_two one_three_two three_one one_four four_three_one