Here's a slightly shorter version that I think does the same thing, using the regex engine to do all the iteration:

#!/usr/bin/perl # Palindrome sequence http://perlmonks.org/?node_id=1117312 use strict; use warnings; my $contig; # section title my %palhash; my $count = 0; while(<DATA>) { $count++ < 1000 or last; /Contig/ and $contig = $_; /([CAGU]{5,20}) # left part ([CAGU]{0,15}) # middle part ((??{reverse($1)=~tr|CAGU|GUCA|r})) # reverse (right) match (??{$palhash{$contig}{"$1*$2*$3"}++})^/x; # count found, fail } for my $contig ( sort keys %palhash ) { print $contig; for my $key ( sort keys %{ $palhash{$contig} } ) { print "$key => $palhash{$contig}{$key}\n"; } print "\n"; } __DATA__ Contig1 NAAAAGUAUAGGCUCGAGAGAGAAGUCCUGGCCUAUCGGAUUACACCACGNGUCAGAUCU GUCACUUCAAGGAGCUUUUCAGCGUCUUUGACAAGAAUGGCNGACGGUUGCCGUCCUUCC AGGGAGAUUGGGGCAGAAUUCGAGUCACUGCCNGUGGAUUCCUUAGUGAUCCAGAUGUCG GAUAUAUGUCCAAUAUAGCCAAUNGUUGUUGGACAUAGCACCAUCAAUCAUCAAGAUUGC UAUUUCGCCACCUCNAUGUAGAGUGGAAGAUCCAAAUGCGGGAAUGGAUUUCAAGGUUGG UUAAGNAUGGGGGCAGCGGAUGAGUUAACAGUACAGCAAGCUAGGCAGCUCCAUUUNUGA CUUGGAAUCUUCAUACGAUUCAUUUAUGGCAGCUUUGCCUAAUGUUGNGUACUUGAAAGU AAAAUCCAAUAUUUCCAUAUUUUUGUUUAGUGCUUCAAAUCUGGUGAGAACGGAUGUUUU AAGUUGGUAAAACAGUGUAUUCAUUUUGAAGAGUUCUUGGAUUGUUUAAAGCUCAAGAUG CUAUUUGUGUGCUGCUGAUUUGUCGUUCUAUGAGAAAGAAUAUAUGCUUUAAUUUGGUUU UGUAAUAUUAANUAUAUUCAUUCCCCUUGAUUGUUGUUUUGUNNNNNNNNNNNNNNNNNN NN Contig2 NUGUUUUUUCUUUACUCGGUGUCUCGUCGGUACUCGACGACUA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GAA*ACCCC => 1 UAUAU*CUUUAAUU*AUAUA => 1 UCGUC*GGUACUC*GACGA => 1 UCGUCG*GUACU*CGACGA => 1

By the way, there may be a problem with overlapping sequences. Your initial solution doesn't look like it allows them.


In reply to Re: Palindrome sequence from file containing mutliple sequences by Anonymous Monk
in thread Palindrome sequence from file containing mutliple sequences by reciter

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.