There are two things that make this much easier. One, is to iterate over the data twice, the first time to set up your relationships, the second time to report their existance. And two, rely solely on incrementing +1, as it is the easier of the relationships to calculate because of the special magic of ++ on strings.
You still must define the characteristics of your boundary conditions though. Ideally, we want the increment and decrement relationships to be one to one. What is the increment of "Bar999"? If it is "Bar1000", then your relationship is no longer 1 to 1 as there is also "Bar0999".
Anyway, here is an implementation without the boundary conditions fully defined. I've added two data entries for lines that do not match:
use Fcntl qw(SEEK_SET);
use strict;
# Calculate Relationships.
# - Rely on increment, as it's the easier of the two to calculate.
my %decrement;
my %increment; # Synonymous with existance.
my $start_of_DATA = tell DATA;
while (<DATA>) {
chomp;
my $item = $_;
# Note concerning parsing
# - This regex requires that a prefix exist.
if ($item =~ m{ (\w+) ( (?<!\d)\d+ | (?<![A-Z])[A-Z]+ ) \z}x) {
my ($prefix, $suffix) = ($1, $2);
# Note: this is the primary spot where there might be changes
+in rules.
# - What happens when the character ends in 'Z'? Currently
# That would translate to 'AA'.
# - What happens when number is 999? Currently that would tra
+nslate
# to '1000'.
# Fix the rules here, and everything else will translate.
(my $suffix_next = $suffix)++;
my $item_next = $prefix . $suffix_next;
$increment{$item} = $item_next;
$decrement{$item_next} = $item;
} else {
die "Invalid data: $_";
}
}
# Reparse DATA
seek(DATA, $start_of_DATA, SEEK_SET);
while (<DATA>) {
chomp;
print;
if ($increment{ $decrement{ $_ } }) {
print ";$decrement{$_}";
} elsif ($increment{ $increment{ $_ } }) {
print ";$increment{$_}";
}
print "\n";
}
__DATA__
AAA30
BBC5
SHT12H
DAL33B
BBC49
AAA31
BBC8
BBC3
DAL33A
BBC6
SHT12G
BBC50
And the output is:
>perl scratch.pl
AAA30;AAA31
BBC5;BBC6
SHT12H;SHT12G
DAL33B;DAL33A
BBC49;BBC50
AAA31;AAA30
BBC8
BBC3
DAL33A;DAL33B
BBC6;BBC5
SHT12G;SHT12H
BBC50;BBC49
- Miller
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.