Perl help regarding hash
Hello,
I've created a hash, of codons and amino acids. My program should allow a user to input a DNA codon and then output the appropriate single letter code for the amino acid corresponding to that codon. If the input does not correspond to an amino acid, then the output should say so. I'm stuck. Here is all my code
my %codon= ();
while(<DATA>){ # while loop to read line by line was is in __DATA__
chomp; #remove newline characters
my ($key, $lettercode)= split; #we split the key value pairs on each line
$codon{$key} = $lettercode; # we assign the key value pairs to %codon in scalar fasion
}
#print out all pairs
while (my($k,$l)= each(%codon)){print"$k =>$l\n";
#print to see that hash was created with data sucessfully
# input output command to generate letter code if (exists()
}
__DATA__
TTT F
CTT L
ATT I
GTT V
TTC F
CTC L
ATC I
GTC V
TTA L
CTA L
ATA I
GTA V
TTG L
CTG L
ATG M
GTG V
TCT S
CCT P
ACT T
GCT A
TCC S
CCC P
ACC T
GCC A
TCA S
CCA P
ACA T
GCA A
TCG S
CCG P
ACG T
GCG A
TAT Y
CAT H
AAT N
GAT D
TAC Y
CAC H
AAC N
GAC D
TAA Stop
CAA Q
AAA K
GAA E
TAG Stop
CAG Q
AAG K
GAG E
TGT C
CGT R
AGT S
GGT G
TGC C
CGC R
AGC S
GGC G
TGA Stop
CGA R
AGA R
GGA G
TGG W
CGG R
AGG R
GGG G
• 3,562 views
•
link
1 answer
#!/usr/bin/perl -w
my %codon= (); while(<DATA>){ # while loop to read line by line was is in __DATA__
chomp; #remove newline characters
my ($key, $lettercode)= split; #we split the key value pairs on each line
$codon{$key} = $lettercode; # we assign the key value pairs to %codon in scalar fasion
}
#print out all pairs
while (my($k,$l)= each(%codon)){
print"$k \t $l\n"; #print to see that hash was created with data sucessfully
}
#input output command to generate letter code
print " Please enter codon\n" ;
my $userin = <STDIN> ;
chomp $userin ;
if (exists($codon{$userin}) ) {
my $code = $codon{$userin} ;
print "$userin is $code \n" ; exit ;
}
else {
print "$userin does not exist\n" ; exit ;
}
__DATA__
TTT F
CTT L
ATT I
GTT V TTC F
CTC L
ATC I
GTC V TTA L
CTA L
ATA I
GTA V TTG L
CTG L
ATG M
GTG V TCT S
CCT P
ACT T
GCT A TCC S
CCC P
ACC T
GCC A TCA S
CCA P
ACA T
GCA A TCG S
CCG P
ACG T
GCG A TAT Y
CAT H
AAT N
GAT D TAC Y
CAC H
AAC N
GAC D TAA Stop
CAA Q
AAA K
GAA E TAG Stop
CAG Q
AAG K
GAG E TGT C
CGT R
AGT S
GGT G TGC C
CGC R
AGC S
GGC G TGA Stop
CGA R
AGA R
GGA G TGG W
CGG R
AGG R
GGG G
• 0 views
•
link
Log in to answer this question.
Check this program and try to find where your mistake is.
Please review the content before sharing links to sites like this. That code is awful (it won't even compile with a recent Perl, and doesn't answer the question) and the site is pretty sketchy. We can use github/gist for sharing code, that way you can embed the code in your post.
Format your code to make it more readable and enclose it within code tags,