in reply to Listing occurence of all residues

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)