... should result in:#!/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']);
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.:
oruse Data::Dumper; [...] if (exists $fastas{$gene_name}) { $sequence = $fastas{$gene_name}; } else { die Data::Dumper->Dump([$gene_name, \%fastas], ['gene_name', 'fastas +']); }
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.use Data::Dumper; [...] exists $fastas{$gene_name} or die Data::Dumper->Dump([$gene_name, \%fastas], ['gene_name', 'fas +tas']); $sequence = $fastas{$gene_name};
In reply to Re^3: hash access with a variable
by Monk::Thomas
in thread hash access with a variable
by LostWeekender
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |