in reply to hash access with a variable

Print the value of $gene_name, preferably between some visible delimiter (like |). Also, "I get errors" is not very helpful when trying to diagnose a problem. Something along the lines of:

I have this code

$sequence=$fastas{ENSG0000017672};,

but when I do this:

my $gene_name = 'frobnitz'; $sequence=$fastas{$gene_name};

I get the following error

'frobnitz' will cause the destruction of the universe.  Stopping.

I expected to see ...

There is a lot more information there for those who wish to help you to go on. If I were to guess (and that is all this is), I would guess that you are reading the value from STDIN and forgetting to chomp, chop, or s/[\012\015]//g the input value.

--MidLifeXis

Replies are listed 'Best First'.
Re^2: hash access with a variable
by LostWeekender (Novice) on Aug 06, 2014 at 17:57 UTC

    Printing $gene_name produces what I expect (ENSG0000017672) and I am chomping the input.

    The error I get is:

    Use of uninitialized value $sequence in concatenation (.) or string at + startend-mod3.pl line 48, <FILE1> chunk 1. Use of uninitialized value $sequence in substr at startend-mod3.pl lin +e 49, <FILE1> chunk 1

    lines 48 & 49 are:

    print "$sequence\n"; my $sub_sequence = substr $sequence, ($startres - 11), ($endres - $ +startres + 11);

    Thanks again

      This shouldn't be possible. If $gene_name contains precisely the string "ENSG0000017672", then $fastas{ENSG0000017672} and $fastas{$gene_name} will both compute to the exact same value.

      So either $gene_name is getting mangled before your hash access, or $sequence afterwards. Could you share some more of your code so we can try and figure out where it's going wrong?

      When line 48 of your script is this:
      print "$sequence\n";
      And the warning that perl gives you is "use of uninitialized value $sequence in … string", it means that as of line 48 in your script, no value has ever been assigned to the variable $sequence.

      If that fact does not agree with your assumptions about the state of things when your script reaches line 48, then your assumptions are wrong, and this is probably related to the fact that when you try to use $sequence as a hash key, you don't get what you expect.

      You could try forming a minimal, self-contained, runnable (small) script that demonstrates the problem you're having, and if that exercise by itself doesn't reveal the problem for you, post that script in full.

        ... no value has ever been assigned to the variable $sequence.

        No, it means $sequence is undef at that point, which was probably assigned to it by the statement the OP is having trouble with, $sequence=$fastas{$gene_name};. ($sequence could very well have held a value before then.)

        ... when you try to use $sequence as a hash key...

        They're not, you probably mean $gene_name.

      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.