#!usr/bin/perl use strict; use warnings; use vars qw (@fields $field $seen); while () { chomp $_; push @fields, $_; } for $field(@fields) { $seen = $field; our $base_seen = substr($seen, 0, -1); # all but last char of $seen our $root = chop($seen); # get last char of $seen test1($root, $base_seen); } #####subs sub test1 { # Excluding the last char, test whether the fields match for $field(@fields) { our $base_field = substr($field, 0, -1); # all but last char of $field if ($main::base_seen eq $base_field) { test2($main::root, $main::base_seen, $field); } } } sub test2 { # bases matched, now test last char for a match +/- 1 my $root = shift(@_); my $t1 = (shift(@_) . $root); my $test1 = shift(@_); if ( $t1 eq $test1 ) { # Returning, t1 is identical to test1 (haven't figured out how to skip this comparison entirely) return; } else { # print "\nIn else of test2, ready to cmp: $t1, $test1\n"; my $rev_last_t1 = reverse($t1); # reverse, for ease of m// my $rev_last_test1 = reverse($test1); #test "last" char which is now the initial char in $rev_last_t1 via reverse my $last_rlt1 = ord($rev_last_t1 =~ /(.)/); # numify alphas for +/- below my $last_test1 = ord($rev_last_test1 =~ /(.)/); if ( $last_rlt1 == ( ($last_test1++) || ($last_test1--) ) ) { print "same base and adjacent terminal chars: $t1;$test1\n"; } } } # end # one value added to OP's data: __DATA__ AAA30 BBC5 SHT12H DAL33B BBC49 AAA31 DAL33A BBC6 SHT12G BBC50 AAA37 AAA29 #### same base and adjacent terminal chars: AAA30;AAA31 same base and adjacent terminal chars: AAA30;AAA37 # False positive same base and adjacent terminal chars: BBC5;BBC6 same base and adjacent terminal chars: SHT12H;SHT12G same base and adjacent terminal chars: DAL33B;DAL33A same base and adjacent terminal chars: AAA31;AAA30 same base and adjacent terminal chars: AAA31;AAA37 # False positive same base and adjacent terminal chars: DAL33A;DAL33B same base and adjacent terminal chars: BBC6;BBC5 same base and adjacent terminal chars: SHT12G;SHT12H same base and adjacent terminal chars: AAA37;AAA30 # False positive same base and adjacent terminal chars: AAA37;AAA31 # False positive