Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

how extract values of hash via key

by sahusonu (Initiate)
on Nov 22, 2013 at 09:18 UTC ( #1063902=perlquestion: print w/ replies, xml ) Need Help??
sahusonu has asked for the wisdom of the Perl Monks concerning the following question:

I m woking on protein coding problem . one amoni acid code for more than codon.suppose
%CodonMap = ( 'GCA'=>'A', 'GCC'=>'A', 'GCG'=>'A', 'GCU'=>'A') %reverse=reverse(%CodonMap); for($i=0;$i<4;$i++) { $value.=$reverse{'A'}; print "$value\n"; }
how to get all different codon for A.

Comment on how extract values of hash via key
Download Code
Replies are listed 'Best First'.
Re: how extract values of hash via key
by Corion (Pope) on Nov 22, 2013 at 09:39 UTC
    %reverse= reverse(%CodonMap)

    This does not do what you think it does.

    A hash key in Perl can only have one value. Use Data::Dumper to look at the result:

    use Data::Dumper; %reverse= reverse(%CodonMap) print Dumper \%reverse;

    You will need to construct your reverse map manually, for example by looping through all keys and values:

    use Data::Dumper; my %reverse; while( my( $k,$v )= each %CodonMap) { $reverse{ $v }||= []; push @{ $reverse{ $v }}, $k; }; print Dumper \%reverse;

    Then, you will need to adapt your program logic to cater for the fact that you don't have simple scalars in %reverse anymore.

Re: how extract values of hash via key
by hippo (Vicar) on Nov 22, 2013 at 09:47 UTC

    Keeping your initial declaration of the hash, we can do:

    my %CodonMap = ('GCA'=>'A', 'GCC'=>'A', 'GCG'=>'A', 'GCU'=>'A'); my @acodes = (); for my $key (keys %CodonMap) { next unless $CodonMap{$key} eq 'A'; push @acodes, $key; } print join ("\n", @acodes, '');

    There's probably a more efficient way, but at least you now have a working algorithm.

      grep is doing the job nicely:

      my @acodes = grep { $CodonMap{$_} eq 'A' } keys %CodonMap;
Re: how extract values of hash via key
by mendeepak (Scribe) on Nov 22, 2013 at 09:45 UTC

    Site itself asks this before posting

    Use:  
    <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post
    

    It will be useful for others to read.
    And about your question please reffer this multiple values for one key in hash, might be useful.

    *=*=*dEEPAk*=*=*
Re: how extract values of hash via key
by BillKSmith (Deacon) on Nov 22, 2013 at 19:01 UTC
Re: how extract values of hash via key
by kcott (Abbot) on Nov 22, 2013 at 23:42 UTC

    G'day sahusonu,

    Welcome to the monastery.

    Your simplest solution might be this:

    my %reverse; push @{$reverse{$CodonMap{$_}}}, $_ for keys %CodonMap;

    Here's an example with some additional (bogus) data to show this is extensible:

    #!/usr/bin/env perl -l use strict; use warnings; my %CodonMap = (GCA=>'A', GCC=>'A', GCG=>'A', GCU=>'A', UVW=>'B', XYZ= +>'B'); my %reverse; push @{$reverse{$CodonMap{$_}}}, $_ for keys %CodonMap; print "A codons: @{$reverse{A}}"; print "B codons: @{$reverse{B}}";

    Output:

    A codons: GCA GCU GCG GCC B codons: UVW XYZ

    -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1063902]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2015-07-30 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (270 votes), past polls