The main problem of your code has been solved. However, there are lots of other things that can be improved:
- Use the 3 argument version of open with lexical filehandles. Also, the open or die idiom is much more readable and common than your unless structure.
- Do not use the same name for various types of things (scalar and array variable in this case).
- If you can process the file line by line, do not slurp it all into memory.
- Use hashes to count numbers of occurrences.
Here is how I would approach your problem:
#!/usr/bin/perl
use strict;
use warnings;
print "Enter the file containing the sequence: ";
my $filename = <STDIN>;
chomp $filename;
open my $FH, '<', $filename or die "Cannot open $filename: $!";
my @chars = qw(A C D E F G H I K L M N P Q R S T V W Y);
my $length;
my $char_regex = join q(), @chars;
$char_regex = qr/[$char_regex]/;
my %occ;
while (my $line = <$FH>)
{
for my $char (split //, $line)
{
next unless $char =~ $char_regex;
$length++;
$occ{$char}++;
}
}
print "AMINO ACID \t OCCURRENCE \t FREQUENCY\n";
for my $char (@chars)
{
$occ{$char} //= 0;
print "$char \t\t $occ{$char} \t\t ", $occ{$char} / $length, "\n"
+;
}
BTW, the plus sign at 'S' just means your line is too long and has been wrapped. You can adjust your line length in your settings.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|