Late as usual I see...
Here is my suggestion
#!/usr/bin/perl
use strict;
{
local %_;
while(<DATA>){
chomp;
if(/(\d+|[A-Z])$/){
$_{substr($_,0,(length($_)-length($1)))}->{$1} = $_;
}
}
for(sort keys %_){
if(scalar(keys %{$_{$_}}) % 2 == 0){
local @_ = sort{$a <=> $b || ord($a) <=> ord($b)}keys %{$_
+{$_}};
for(my $i = 0; $i < scalar(@_); $i+=2){
if( ($_[$i] =~ /\d$/ && abs($_[$i] - $_[$i+1]) == 1) |
+| (abs(ord($_[$i]) - ord($_[$i+1])) == 1) ){
print $_{$_}->{$_[$i]}.";".$_{$_}->{$_[$i+1]}."\n"
+ ;
print $_{$_}->{$_[$i+1]}.";".$_{$_}->{$_[$i]}."\n"
+ ;
}
}
}
}
}
__DATA__
AAA30
BBC5
SHT12H
DAL33B
BBC49
AAA31
BBC8
BBC3
DAL33A
BBC6
SHT12G
BBC50
Output
AAA30;AAA31
AAA31;AAA30
BBC49;BBC50
BBC50;BBC49
DAL33A;DAL33B
DAL33B;DAL33A
SHT12G;SHT12H
SHT12H;SHT12G
Not exactly the OP's output but close...
Just a thought :)
-InjunJoel
"I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|