Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How can I solve this hash issue?

by adur (Initiate)
on Jun 16, 2016 at 19:42 UTC ( [id://1165899]=perlquestion: print w/replies, xml ) Need Help??

adur has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm wondering how reliable perl comparisons and hashes are, and if there's something I can do instead or fix. I'm using perl 5.16.1, 5.16.2, or 5.18.2.

I want to find gene names in a bed file with multiple assigned ensembl ids, and regular gene names with multiple ensembl ids. For easier reading, x with multiple z, and y with multiple z. I want to push those values into the respective arrays. These are the variables:

my %bedgenenames; my @bedrepeats; my %realgenenames; my @haplotyperegionrepeats;

When I try this, I get the correct outputs for the first statement and incorrect outputs for the second.

chomp($entry); my @array = grep {$_} split /[\t ]/, $entry; unless(exists($bedgenenames{"$array[3]"})){ $bedgenenames{"$array[3]"}="$array[6]"; }elsif("$bedgenenames{$array[3]}" ne "array[6]"){ push @bedrepeats, "$array[3]"; print "$array[3] for $bedgenenames{$array[3]} and $array[6] bed\n" +; } unless(exists($realgenenames{"$array[8]"})){ $realgenenames{"$array[8]"}="$array[6]"; }elsif("$realgenenames{$array[8]}" ne "array[6]"){ push @haplotyperegionrepeats, "$array[8]"; print "$array[8] for $realgenenames{$array[8]} and $array[6] haplo +type\n"; }

This is the output for the first(correct, where the two ensembl ids are different) and second(incorrect, where the two ensembl ids are the same):

WASH5P14829 for ENSG00000223972 and ENSG00000227232 bed WASH7P for ENSG00000227232 and ENSG00000227232 haplotype
How can I solve this?

Replies are listed 'Best First'.
Re: How can I solve this hash issue?
by talexb (Chancellor) on Jun 16, 2016 at 20:30 UTC

    In addition, you are unnecessarily putting your variables inside double quotes. So your line

    $bedgenenames{"$array[3]"}="$array[6]";
    can just be
    $bedgenenames{$array[3]}=$array[6];

    And I echo what someone else has said already -- get friendly with the debugger. It's most illuminating to step through your program and see what's going on.

    Finally, Perl hashes may not be perfect, but they are pretty reliable. If your code isn't behaving correctly, it's most likely that the fault is with your code, and not Perl itself.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Re: How can I solve this hash issue?
by GotToBTru (Prior) on Jun 16, 2016 at 20:17 UTC

    Missing the $ before array[6] in the second elsif.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

      Double quotes are not only useless here, they also prevented Perl from telling you what the problem was.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        I didn't include them before, but didn't get an error either. I don't know why the first one was coming out correct.
      It's always that, isn't it? Anyway, thank you so much. This saved me so much time.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-20 03:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found