in reply to searching for strings
Warning: I'm not sure it will work with unicode-strings and anyhow it will only take into account the last character (unless the pattern ends in a one or more digits). So @ precedes A and [ follows Z.use strict; my $plus; my $minus; while (<DATA>) { chomp; my $next = next_seq($_); my $previous = previous_seq($_); print "$_, $next, $previous\n"; } sub next_seq { my $pattern = shift; chomp $pattern; if ($pattern =~ m/(.*?)(\d+)$/) { return $1 . ($2 + 1); } else { $pattern =~ m/(.*)(.)/; return $1 . chr(ord($2) + 1); } } sub previous_seq { my $pattern = shift; chomp $pattern; if ($pattern =~ m/(.*?)(\d+)$/) { return $1 . ($2 - 1); } else { $pattern =~ m/(.*)(.)/; return $1 . chr(ord($2) - 1); } } __DATA__ AAA30 BBC5 SHT12H DAL33B BBC49 AAA31 DAL33A BBC6 SHT12G BBC50 ABCZ
Now all you have to do is go through the list and search for items which match the next or previous values.
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
|
|---|