Perl help regarding hash
1
0
Entering edit mode
8.2 years ago
ksi216 ▴ 80

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
perl • 2.7k views
ADD COMMENT
2
Entering edit mode

Format your code to make it more readable and enclose it within code tags,

like this.
ADD REPLY
1
Entering edit mode

Check this program and try to find where your mistake is.

ADD REPLY
0
Entering edit mode

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.

ADD REPLY
2
Entering edit mode
8.2 years ago
Zaag ▴ 860
#!/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
ADD COMMENT

Login before adding your answer.

Traffic: 2481 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6