If you want to record the contigs, don't save just the palindromes in the hash, but group them by contig first:
#!/usr/bin/perl use warnings; use strict; open my $IN, '<', 'sample.txt' or die $!; my $pat = qr/^(Contig *([0-9]*))\s/; my $count = 0; my @regexes; for my $n (5 .. 20) { my $re = qr /[CAGU]{$n}/; $regexes[$n-5] = $re; } my %palhash; my $contig; LINE: while ($count < 1000) { my $line = <$IN> ; defined $line or last; $contig = $line if $line =~ /$pat/; ++$count; for my $value (@regexes) { my $start = 0; while ($line =~ /$value/g) { my $endline = $'; my $match = $&; my $revmatch = reverse($match); $revmatch =~ tr/CAGU/GUCA/; if ($endline =~ /^([CAGU]{0,15})($revmatch)/) { $start = 1; my $palindrome = $match . "*" . $1 . "*" . $2; $palhash{$contig}{$palindrome}++; } } next LINE if $start == 0; } } close $IN; for my $contig (keys %palhash) { print $contig; while (my ($key, $value) = each (%{ $palhash{$contig} })) { print "$key => $value\n"; } }

I changed some parts of the code as well, e.g. 3 argument open or die, lexical filehandles, don't process if the input is short, no goto. I don't understand the requirements in detail, but moving away from $' and $& would be a good idea, too.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

In reply to Re: Palindrome sequence from file containing mutliple sequences by choroba
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.