# get prefix sub prefix { my @w = length($_[0]) > length($_[1]) ? reverse @_ : @_; my $len = 1; my $f; $len++ while $w = substr($w[0],0,$len) # and $w[1] =~ /^$w/ # no m// needed, index is fine and 0 == index( $w[1],$w) and $len <= length $w[0] and $f = $w; return if $f eq $w[0]; $f; } # get suffix sub suffix { my @w; push @w, scalar reverse($_) for @_; $ret = reverse prefix(@w); } while() { # DATA section contains names as per OP's link push @list, (split/,/)[0]; } # get prefixes and suffixes my (%names, %pre, %suf); $names{$_}++ for @list; # updated per [id://759430] while(@list) { my $first = $ARGV[0] || shift @list; # updated per [id://759430] # next if $names{$first}++; # store for convenient lookup for my $name (@list) { my $s = prefix($first,$name); ++$pre{$s}->{$first} and ++$pre{$s}->{$name} if $s; my $s = suffix($first,$name); ++$suf{$s}->{$first} and ++$suf{$s}->{$name} if $s; } last if $ARGV[0]; # updated per [id://759430] } # get pairings. Concatenate each prefix which each suffix # and see if it comes to a compliment, er, a complement # which can be looked up in the %names hash my @pre = keys %pre; my @suf = keys %suf; for my $pre(@pre) { for my $suf (@suf) { if ($names{$pre.$suf}) { print "found: prefix(@{[keys %{$pre{$pre}}]}) = $pre, " . "suffix(@{[keys %{$suf{$suf}}]}) = $suf, " . "$pre.$suf = $pre$suf\n"; } } }