Thanks for the update; it would be good if you could put your sample input and output inside <code> tags as mentioned previously. Note that the regular expression /^>sp\|(\w+\S+)\|/ does not match the sample data you provided, but I'm guessing that's just because the sample data is oversimplified. Some general things you should do:

  1. Use strict and warnings - important!
  2. Use perltidy
  3. Have a look at the Basic debugging checklist

The code still has a couple of smaller issues, but the two things that are preventing it from working correctly are:

  1. When you write while ( ($header, $Fasta_split{$header}) = each(%Fasta_split) ) {, you are assigning to / re-using variables that you shouldn't re-use. You could improve this loop a bit using for, sort and keys: for my $header (sort keys %Fasta_split) { (but note you actually shouldn't re-use the variable name $header here!)
  2. You are re-using %count without clearing it first. The easiest way to fix it is to use a %count local to the loop, i.e. put my %count; inside the second loop.

The output can be made to be a bit more organized also using the same method as above: for my $w (sort keys %count) { print "$w:$count{$w}\t"; } - But of course it's possible to get the output to look even more like what you want it to. The following code loops over the letters you want to output, and prints either the number, or a dash if that number is zero (or undefined). The list of letters could be simplified with Perl's qw//.

for my $w ("a","b","c","d","g","u","x","j","k") { print $count{$w}||"-", " "; }

Which gives:

5 4 4 3 - - - - - - - - - 5 - - 3 4

Customizing that is left as an exercise to the reader :-) (one tip: see uc and lc)


In reply to Re: Listing occurence of all residues by Anonymous Monk
in thread Listing occurence of all residues by sreya

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.