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


    In reply to Re: searching for strings by Anno
    in thread searching for strings by steph_bow

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.