Hello,

Further to my problems i posted yesterday i cannot seem to get the program to recognise information stored in one file and compare it to another file.

This is some of the code a colleague gave me to get it to recognise an Accession number in one file and compare it to an element in an array, obtained from a second file.

The $ref_filehandle holds the data from the reference file that contains only a list of Accession numbers, i.e.

AB014570.2 AB055861.1 AB067522.1 AB073617.1 AC004166.12 AC004851.2 AC004867.5 AC004883.3 AC005077.5 AC005080.2
Where as the @subjects holds in each element :
gi|14670349|ref|NM_032999.1| Homo sapiens general transcription factor II, i (GTF2I), transcript .... or a derivetive of.

The problem i have is that the hash %refList holds all the accession numbers in KEY = Accession VALUE = Accession, so both accession values are stored in key and value - should be an array i know but i need speed when searching through for each element of @subjects, hence the use of "defined" function.
The only problem is that the " if " statement highlighted always equates to false....WHY?

I have found, using print statements, that the hash does have the right values stored in it, but when searched for $elements[3] element it cant find it, so i tried "! defined" - and guess what, it equates to true. Knowing that the values are stored in the hash i can only assume the problem lies with the if statement.

sub get_Results() { open( NEWACC, ">ref_files/$newRefFile" ) || die "$!"; open( NEWALIGN, ">blast_files/$newBlastFile") || die "$!"; ## put list of reference accessions into a structure so we can parse i +t... my %refList = (); while (<$ref_filehandle>) # used instead of REFFILE { chomp; # removes newline character my $key = $_; $refList{ $key } = "$_"; } #PARSE BLAST RESULT HERE.... #read results file one line at a time my @resultLine = <$blast_filehandle>; # used instead of BLASTFILE my $alignment = {}; # reference to an empty hash my @subjects; # reference to empty array my $current_subject = "front_matter"; $alignment->{$current_subject} = ""; for (my $i = 0 ; $i<scalar @resultLine; $i++) { if ($resultLine[$i] =~ /^>/) { $current_subject = $resultLine[ $i ]; chomp ($current_subject); push (@subjects, $current_subject); $alignment->{$current_subject} = ""; } $alignment->{$current_subject} = $alignment->{$current_subject}.$r +esultLine[ $i ]; } my @elements; foreach my $z (@subjects) { chomp $z; @elements = split('\|', $z); if ( ! defined $elements[ 3 ] ) { print ("Parsing Error<BR>"); print ("Line $z"); } ##### PROBLEM LIES HERE WITH IF STATEMENT if (defined $elements[3] && defined $refList{$elements[3]} ) { print ("Already Present in file"); } else { print "No match in reference file"; print (NEWALIGN $alignment->{$z}."\n"); print (NEWACC $elements[3]."\n"); } } #close files close NEWALIGN; close NEWACC; }

Any help is really appreciated - including any critical appraisal as its not my handy work.

Cheers people
MonkPaul.


In reply to If statement problem with hash values by MonkPaul

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.