#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
## create a hash all at once
my %genetic_code = (
"ATG" => "Met",
"AAA" => "Lys",
"CCA" => "Pro",
);
## print a singe key/value pair
print "Before/ATG: " , $genetic_code{"ATG"} ,"\n";
##print out the entire hash one key/value pair at a time
foreach my $codon (keys %genetic_code){
## get, or retrieve, a value using a key
my $aa = $genetic_code{$codon};
print "$codon translates to $aa\n";
}
## overwrite or reset a single key's value
$genetic_code{"ATG"} = "start_codon";
## print a single/key value pair after reset
print "After/ATG: " , $genetic_code{"ATG"} ,"\n";
## print the entire hash after reset
foreach my $codon (keys %genetic_code){
my $aa = $genetic_code{$codon};
print "$codon translates to $aa\n";
}
## what are other ways to print out an entire hash?
## in and outside of quotes is useless:
print "---other ways to print out an entire hash---\n";
print "Printing the hash within quotes does not work: %genetic_code\n";
print "Printing outside of quotes is swished and pretty useless: ", %genetic_code,"\n";
## Data::Dumper is useful for debugging
print "Print hash using the module Data::Dumper:\n";
## make sure to use the '\' before '%'. can also use to print arrays: \@array
print Dumper \%genetic_code , "\n"; |
Comments are closed.