Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: If statement problem with hash values

by thundergnat (Deacon)
on Jun 15, 2005 at 18:40 UTC ( #467012=note: print w/replies, xml ) Need Help??


in reply to If statement problem with hash values

I have a feeling that the values in %refList are not what you expect. I notice that your example data has an underscore but none of the example $ref_filehandle values do. Could it be that you need to strip out the underscore?

A better check in the if statement may be to see if the hash value exists and then whether it is equal to the array value to avoid autovivifying non matched values.

I rewrote your snippet to run stand alone locally with some bogus data based on your examples. Does this do what you expect?

use warnings; use strict; my @resultLine = ( 'gi|14670349|ref|NM_032999.1| Homo sapiens general transcription facto +r II, i', 'one', 'gi|14670348|ref|NM_032998.1| Homo sapiens general transcription facto +r II, ii', 'two', 'gi|14670347|ref|NM_032997.1| Homo sapiens general transcription facto +r II, iii', 'three', 'gi|14670346|ref|NM_032996.1| Homo sapiens general transcription facto +r II, iv', 'four', 'gi|14670345|ref|NM_032995.1| Homo sapiens general transcription facto +r II, v', 'five', 'gi|14670344|ref|NM_032994.1| Homo sapiens general transcription facto +r II, vi', 'six', 'gi|14670343|ref|NM_032993.1| Homo sapiens general transcription facto +r II, vii', 'seven' ); my %refList; my @subjects; my $alignment = {}; while (<DATA>) { chomp; $refList{ $_ } = $_; } my $current_subject; for (@resultLine) { if (/^gi\|/) { $current_subject = $_; chomp $current_subject; push (@subjects, $current_subject); } $alignment->{$current_subject} .= $_; } for my $z (@subjects) { my @elements = split('\|', $z); if ( ! defined $elements[ 3 ] ) { print ("Parsing Error<BR>"); print ("Line $z"); } if (exists $refList{$elements[3]} and $refList{$elements[3]} eq $e +lements[3]) { print ("Already Present in file. - $elements[3]\n"); } else { print "No match in reference file. - "; print ($alignment->{$z}."\n"); print ($elements[3]."\n"); } } __DATA__ AB014570.2 AB055861.1 AB067522.1 AB073617.1 AC004166.12 AC004851.2 AC004867.5 AC004883.3 AC005077.5 AC005080.2 NM_032997.1 NM_032993.1 NM_032999.1

Replies are listed 'Best First'.
Re: If statement problem with hash values
by MonkPaul (Friar) on Jun 15, 2005 at 19:29 UTC
    Sure,

    That seems to be what i need -> if (exists $refList{$elements3} and $refList{$elements3} eq $e +lements3)

    I like the way you got the current_line variable also, very nifty.

    I will let you know when i implement it - i had a beer to relax this aft, sat in the sun and now i cant be bothered to look at it again.
    Thanks
    MonkPaul.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://467012]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2023-02-06 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (37 votes). Check out past polls.

    Notices?