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

Hi everyone. I've got a DNA sequence that I've transformed into an amino acid sequence using this loop ($dohg is the placeholder for the DNA sequence; I already have a hashtable setup to transform each substring into an amino acid):
for (my $aa = 0; $aa < length($dohg2); $aa=$aa+3) {my $sub8 = substr($dohg2, $aa-1, 3); $lol2=$codon{$sub8}; #print $lol2; push @array4, $lol2; } my $dohg3 = join "", @array4;
If all the DNA/amino acid stuff is confusing, it's not really important: I basically just want to export the hashtable output of my substring (represented by $lol2) as an array by pushing it into an array. Then, once I exit the loop, I want to join that array into a single variable. However, even if I call "my @array4 = 0;" before the loop begins, I keep getting an error when I try to do any downstream work which reads "Use of uninitialized value within @array in join or string". Any ideas?

Replies are listed 'Best First'.
Re: Keep getting "use of unitialized value" error in a weird place
by Lotus1 (Vicar) on Apr 09, 2016 at 21:15 UTC

    Hi listendohg and welcome to Perlmonks.

    Your approach is more of a C programming style which can be made to work but isn't much fun. Try the Perl way and you'll start to see why many of us enjoy Perl so much. It makes easy things easy and hard things possible.

    Disclaimer: I'm not an expert in biology so the following is just an example and probably isn't correct as far as the dna and codons are concerned. I found the information included from

    #!/usr/bin/perl use strict; use warnings; ### random 30 character dna sequence from ### http://www.bioinformatics.org/sms2/random_dna.html my $dohg2 = 'ttcgtgaggcaggctgtcaattagagtcgt'; my @sub8 = ( $dohg2 =~ m/.../g ); print "@sub8\n"; # Codon : a sequence of three nucleotides that together form a unit of + # genetic code in a DNA or RNA molecule. #https://en.wikipedia.org/wiki/DNA_codon_table # From the following site I copied this hash definition: #http://web.cs.iastate.edu/~cs596/notes/perl_hashes.html my %DNA_code = ( 'GCT' => 'A', 'GCC' => 'A', 'GCA' => 'A', 'GCG' => 'A', 'TTA' => 'L', 'TTG' => 'L', 'CTT' => 'L', 'CTC' => 'L', 'CTA' => 'L', 'CTG' => 'L', 'CGT' => 'R', 'CGC' => 'R', 'CGA' => 'R', 'CGG' => 'R', 'AGA' => 'R', 'AGG' => 'R', 'AAA' => 'K', 'AAG' => 'K', 'AAT' => 'N', 'AAC' => 'N', 'ATG' => 'M', 'GAT' => 'D', 'GAC' => 'D', 'TTT' => 'F', 'TTC' => 'F', 'TGT' => 'C', 'TGC' => 'C', 'CCT' => 'P', 'CCC' => 'P', 'CCA' => 'P', 'CCG' => 'P', 'CAA' => 'Q', 'CAG' => 'Q', 'TCT' => 'S', 'TCC' => 'S', 'TCA' => 'S', 'TCG' => 'S', 'AGT' => 'S', 'AGC' => 'S', 'GAA' => 'E', 'GAG' => 'E', 'ACT' => 'T', 'ACC' => 'T', 'ACA' => 'T', 'ACG' => 'T', 'GGT' => 'G', 'GGC' => 'G', 'GGA' => 'G', 'GGG' => 'G', 'TGG' => 'W', 'CAT' => 'H', 'CAC' => 'H', 'TAT' => 'Y', 'TAC' => 'Y', 'ATT' => 'I', 'ATC' => 'I', 'ATA' => 'I', 'GTT' => 'V', 'GTC' => 'V', 'GTA' => 'V', 'GTG' => 'V',); foreach ( @sub8 ) { if( defined $DNA_code{uc $_} ) { print "$_ => $DNA_code{uc $_}\n"; } else { print "$_ => ???\n"; } }

    The result is below:

    ttc gtg agg cag gct gtc aat tag agt cgt ttc => F gtg => V agg => R cag => Q gct => A gtc => V aat => N tag => ??? agt => S cgt => R

      Even more fun

      print "$_ => ", $DNA_code{+uc} // '???', "\n" for @sub8;
Re: Keep getting "use of unitialized value" error in a weird place
by johngg (Canon) on Apr 09, 2016 at 16:50 UTC

    Are you sure you want $aa-1 as the offset in your substr?

    $ perl -Mstrict -Mwarnings -E ' my $str = q{abc} x 3; for ( my $idx = 0; $idx < length $str; $idx += 3 ) { my $three = substr $str, $idx - 1, 3; say $three; }' c cab cab $
    $ perl -Mstrict -Mwarnings -E ' my $str = q{abc} x 3; for ( my $idx = 0; $idx < length $str; $idx += 3 ) { my $three = substr $str, $idx, 3; say $three; }' abc abc abc $

    I know nothing of genetics so that might be what you want but it looks weird.

    Cheers,

    JohnGG

        this will take away the error, but then when i look at the output of the loop it's littered with stop codons. if i print $lol2 from within the loop, i get the amino acid sequences and they look good (stops always followed by M or some alternative start codon), but i still get this error.
Re: Keep getting "use of unitialized value" error in a weird place
by LanX (Saint) on Apr 09, 2016 at 16:46 UTC
    Please try Data::Dumper to check if %codon and @array4 really hold what you think they do.

    @array4=0 is only initializing the first element $array4[0] !

    Most probbaly you are pushing non existing elements from %codon which fill @array4 with undef values.

    see also basic debugging checklist

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

    PS: As a side note: badly formated code like yours is the best publicity for Python... :-/

Re: Keep getting "use of unitialized value" error in a weird place
by 1nickt (Canon) on Apr 09, 2016 at 16:48 UTC

    Hi listendohg,

    However, even if I call "my @array4 = 0;" before the loop begins, I keep getting an error when I try to do any downstream work which reads "Use of uninitialized value within @array in join or string".

    You are declaring the array with an element, 0. So when you later go through the elements, the first one is zero. If your code at that point is testing for truth, it will be an uninitialized value.

    Declare your array so it contains an empty list instead: my @array;.

    When you say "even if I call "my @array4 = 0;" before the loop begins" this indicates that you may not be using strict in your program. You certainly should declare a variable outside a loop (not inside it) if you wish to use it after the loop.

    Finally, if you are storing a hash table as a string and later retrieving it for use again as a data structure, you may want to use JSON.

    Hope this helps!


    The way forward always starts with a minimal test.