in reply to searching for strings

This is a rather complex task. What have you tried so far?

The basic tools would be

  • a pattern that splits a line into a head (AAA BBC DAL33) and a tail (the final number or single character used for pairing). This could be used: my( $head, $tail) = /^(.+?)(\d+|[[:alpha:]])$/; though your description allows for alternatives.
  • One or more hashes to organize the data in a way that the pairings can easily be extracted.
  • Here is one way to do it:

    my (%num, %alpha); while ( <DATA> ) { chomp; my ( $head, $tail) = /^(.+?)(\d+|[[:alpha:]])$/; if ( $tail =~ /^\d+$/ ) { $num{ $head}->{ $tail} = $_; } else { $alpha{ $head}->{ ord $tail} = $_; } } my @pairs = ( map( extract_pairs( $_) => values %num), map( extract_pairs( $_) => values %alpha), ); print "$_\n" for @pairs; exit; sub extract_pairs { my $h = shift; my @pairs; for ( keys %$h ) { for my $partner ( $_ - 1, $_ + 1 ) { if ( exists $h->{ $partner} ) { push @pairs, "$h->{ $_};$h->{ $partner}"; delete @$h{ $_, $partner}; } } } @pairs; } __DATA__ AAA30 BBC5 SHT12H DAL33B BBC49 AAA31 DAL33A BBC6 SHT12G BBC50
    This code will ignore singletons for which a pairing partner cannot be found. If there are more than two consecutive pairs for a single head, say if you had BBC7 besides BBC5 and BBC6 the bevavior is undefined. It will pick some pair(s) and may ignore others.

    Anno