Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

help with hashes

by Anonymous Monk
on Mar 15, 2005 at 11:37 UTC ( #439591=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there. I have always been somewhat confused my hashes in perl and today I've found myself in need of using one. As you can see, I've gotten so far with it, but could someone please advise as to how to print out the 'value' for any given 'key'. So, for example, I wish to use the hash table to look up '1' and print out 'tyrosine'. Thanks. Any help much appreciated.
%aminoacids = ( 1 => 'Tyrosine', 2 => 'Glycine', 3 => 'Leucine', ); for($i = 1; $i<4; $i++) { for($j = 1; $j<4; $j++) { for($k = 1; $k<4; $k++) { # another test print command #print "$i $j $k\n"; # want to print out for example tyrosine tyrosine leucine if $ +i = 1, $j = 1 and $k = 3 } } }

Comment on help with hashes
Download Code
Re: help with hashes
by gaal (Parson) on Mar 15, 2005 at 11:56 UTC
    To subscript a hash, say $aminoacids{ $the_key }. (In your case, the keys are numeric so you may as well be using an array?)

    This interpolates in a string, too, so you can say:

    print "$aminoacids{$i} $aminoacids{$j} $aminoacids{$k}\n";

    Though you can use the more idiomatic hash slice and select several things at once.

    print join " ", @aminoacids{$i, $j, $k}; # note the '@'
Re: help with hashes
by artist (Parson) on Mar 15, 2005 at 12:15 UTC
    If you are generating the lists, you can also use some type of combination module such as Math::Combinatorics.
    use Math::Combinatorics; my @aminoacids = qw(Tyrosine Glycine Leucine); print join("\n", map { join " ", @$_ } permute(@aminoacids)),"\n";
Re: help with hashes
by manav (Scribe) on Mar 15, 2005 at 12:21 UTC
    A shorter way to create loops in Perl is
    %aminoacids = ( 1 => 'Tyrosine', 2 => 'Glycine', 3 => 'Leucine', ); for $i (1..3) { for $j (1..3) { for $k (1..3) { # another test print command #print "$i $j $k\n"; # want to print out for example tyrosine tyrosine leucine if $ +i = 1, $j = 1 and $k = 3 local $"=' ' ; print "@aminoacids{$i,$j,$k}\n" ; } } }

    The loops can also be created by using the keys function.
    for $i (1..3) {
    can and should be changed to
    for $i (keys %aminoacids) {
    and so on for other loops. This is the preferred way of looping through all keys in the hashes.
    Your script can also be implemented through arrays, as you are doing numerical indexing which is faster than associative indexing.

    Manav
Re: help with hashes
by kryberg (Pilgrim) on Mar 15, 2005 at 14:55 UTC
    Get yourself a copy of O'Reilly's Perl Cookbook. I found it very useful for learning how to use hashes and I reference it frequently for many tasks.
Re: help with hashes
by cbrandtbuffalo (Deacon) on Mar 15, 2005 at 18:15 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2014-04-19 16:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls