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??

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

In reply to Re: If statement problem with hash values by thundergnat
in thread 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":



  • 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.
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2023-02-06 10:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (33 votes). Check out past polls.

    Notices?