Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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 } } }

Replies are listed 'Best First'.
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 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.

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 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 cbrandtbuffalo (Deacon) on Mar 15, 2005 at 18:15 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://439591]
Approved by pelagic
[Eily]: you could tie a variable into not having the same value each time, if you like to make people who try to debug your code facepalm
[Corion]: perl -wle 'package o; use overload q("") => sub {warn "str"; ""}, bool => sub{warn "bool"; 1}; package main; my $o={}; bless $o => o; print "Yay" if ($o && !length($o))'
[Corion]: But people writing such code should document the objects they construct and why it makes sense for an object to be invisible as string while being true in a boolean context
[hippo]: That's equal parts clever and horrendous.
[Eily]: the overload version wouldn't return true with "$x" && !length $x though, I guess
[hippo]: The more I look at this code, the more $x is a plain old scalar and the more this condition will never be true. I'm calling it a bug at this point.
[hippo]: Thanks for your input which has soothed my sanity (a little)
[Corion]: Eily: Sure - if you force both things into stringy things, then you break that magic. But that would also mean that you changed the expression, as now $x = 0.00 will be true instead of false as it were before
[Corion]: Ah no, at least in my feeble experiments that doesn't change the meaning
[Corion]: We sell sanity in small packages ;)

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2017-07-27 13:42 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (413 votes). Check out past polls.