The code below is probably inelegant and perhaps even wrongheaded
but I'm posting, in part, TIMTOWTDI; in part, to illustrate some of the well-taken issues with the likes of AAA29 vs AAA30 (above), and in part, because even though this is NOT a solution, $work calls:
#!usr/bin/perl use strict; use warnings; use vars qw (@fields $field $seen); while (<DATA>) { chomp $_; push @fields, $_; } for $field(@fields) { $seen = $field; our $base_seen = substr($seen, 0, -1); # all but last char of $se +en our $root = chop($seen); # get last char of $seen + test1($root, $base_seen); } #####subs sub test1 { # Excluding the last char, test whether the f +ields 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 matc +h +/- 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 t +o 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 e +ase 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
OUTPUT:
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
NOTE1: Fails on AAA29 vs AAA30 and on BBC49 vs BBC50 for (the intended?) reading of OP's definition; if OP had specified "digit" instead of "number" this would not be a failure, but, as is, makes the problem challenging
NOTE2: Fails on AAA37 vs (AAA30 or AAA31) by any reading; this is a glitch in the code /me offers above.
NOTE3: Use any one of several mechanisms, including hashes, to eliminate dupes such as "AAA30;AAA31" and "AAA31;AAA30")
In reply to Re: searching for strings
by ww
in thread searching for strings
by steph_bow
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |