Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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.
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

In reply to Re: Code won't proceed after input. by choroba
in thread Code won't proceed after input. by newbie1991

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (4)
    As of 2014-09-15 02:11 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite cookbook is:










      Results (145 votes), past polls