The following script
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %fastas = ( 'ENSG0000017672' => 'eureka', ); my $gene_name = 'ENSG0000017672'; print 'test1: ', $fastas{ENSG0000017672}, "\n"; print 'test2: ', $fastas{$gene_name}, "\n"; local $Data::Dumper::Useqq = 1; print Data::Dumper->Dump([$gene_name], ['gene_name']);
... should result in:
test1: eureka
test2: eureka
$gene_name = "ENSG0000017672";
If you get the same result then something else is wrong with your code. If not then something is wrong with your perl. The most reasonable explanation is that $gene_name does not contain what you think it should. If there is a mismatch, then perl will not warn you about that, but silently create a new hash key and return 'undef' as it's value. You can explicitely check if a key exists using 'exists $hash{$key}', e.g.:
use Data::Dumper; [...] if (exists $fastas{$gene_name}) { $sequence = $fastas{$gene_name}; } else { die Data::Dumper->Dump([$gene_name, \%fastas], ['gene_name', 'fastas +']); }
or
use Data::Dumper; [...] exists $fastas{$gene_name} or die Data::Dumper->Dump([$gene_name, \%fastas], ['gene_name', 'fas +tas']); $sequence = $fastas{$gene_name};
Both examples are a bit more complicated then strictly necessary but they will not only tell you something is wrong but also what the current content of the relevant variables is.

In reply to Re^3: hash access with a variable by Monk::Thomas
in thread hash access with a variable by LostWeekender

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.