There are many problems with your script.

When you read the lines in from the file

while($_ = <FILENAME>){

They have newlines attached to their ends. And when you store the sequence ID in teh key of the hash:

$accession{$seq} .= $_;

You have not removed it. But when you read the accession iD from the user:

print "Please enter in the accession_ID: "; chomp($accession_id = <STDIN>);

You did remove the newline. So when you try to compare those two:

if ($accession{$g} =~ m/$accession_id/i){

They will not match.

BUT ... when you are trying to compare the id entered by the user with the one read from the file, instead of comparing it with the key (ID) of the hash element:

if( $g =~ m/$accession_id/i){

You are comparing it with the sequence (value) of that hash entry:

if ($accession{$g} =~ m/$accession_id/i){

Which obviously wouldn't work, even if you had removed the newlines from both.

But then, even if you corrected all those problems, there are many other problems with your program.

First you read all the sequences into the hash -- despite that you know you are only looking for one and could stop reading as soon as you've seen that one.

And then you iterate over the hash checking if each of them matches the ID the user entered -- but the whole point of a hash is that you do not need to iterate over it. You can simply look up whether the key you need exists.

Finally, you should take more care over how you format your code. Think of your code as your workbench. If you keep it neat and tidy, you'll find it a lot easier to find what you are looking for.

Also, if you are intending using Perl to do work, rather than as a hobby, I strongly recommend that you take a basic programmers course, or at least work your way through one of the Learning Perl type books available. It would pay you dividends over and over in the long term.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: fasta file by BrowserUk
in thread fasta file by PrincePerl

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.