Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

iterations in a hash

by madM (Beadle)
on Aug 04, 2013 at 14:36 UTC ( #1047798=perlquestion: print w/ replies, xml ) Need Help??
madM has asked for the wisdom of the Perl Monks concerning the following question:

Hi! Im trying to compute an iteration process with a hash for example if i have this hash:
%hash=( A=>4, B=>5, C=>10, D=>2, E=>9 )
i want to multiply the A with the B , then the A with the C .. A with D .. A with E.. and then B with A and so on.. i have been trying to compute that without success .. any help would be really appreciated Thanks!

Comment on iterations in a hash
Download Code
Replies are listed 'Best First'.
Re: iterations in a hash
by BrowserUk (Pope) on Aug 04, 2013 at 15:06 UTC

    That is an inherently recursive process:

    sub crossMul{ my( $r, $first ) = ( shift, shift ); print "$first * $_ = ", $r->{$first} * $r->{$_} for @_; crossMul( $r, @_ ) if @_ > 1; };; %hash=( A=>4, B=>5, C=>10, D=>2, E=>9 );; crossMul( \%hash, sort keys %hash );; A * B = 20 A * C = 40 A * D = 8 A * E = 36 B * C = 50 B * D = 10 B * E = 45 C * D = 20 C * E = 90 D * E = 18

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: iterations in a hash
by rjt (Deacon) on Aug 04, 2013 at 14:54 UTC

    Hello madM! Welcome to PerlMonks.

    i want to multiply the A with the B , then the A with the C .. A with D .. A with E.. and then B with A and so on..

    I'm really not sure what you mean. What output do you expect? The only non-trivial (i.e., pure repetition) case I can think of is that you want to see each product of A*B, A*C, A*D, …, printed separately, like this:

    A * B = 20 A * C = 40 A * D = 8 A * E = 36 B * A = 20 B * C = 50 : :

    Is that what you want? If so, I suggest you have a look at keys, printf, and Foreach Loops, and ponder the following:

    for my $x (sort keys %hash) { for my $y (sort grep { $_ ne $x } keys %hash) { printf "%s * %s = %d\n", $x, $y, $hash{$x} * $hash{$y}; } }

    If you are looking for a different result, please give us an example of the output you are expecting.

Re: iterations in a hash
by Loops (Curate) on Aug 04, 2013 at 15:10 UTC
    my %hash=(A=>4, B=>5, C=>10, D=>2, E=>9); my @zip = map { [ $_, $hash{$_} ] } sort keys %hash; do { my $f = shift @zip; print ("@$f * @$_ = ", $f->[1] * $_->[1], "\n") for (@zip); } while (@zip);
    Or alternatively being a smidgen easier to read:
    my %hash=(A=>4, B=>5, C=>10, D=>2, E=>9); my @zip = sort keys %hash; do { my $f = shift @zip; for (@zip) { my ($v1,$v2) = @hash{$f,$_}; print "$f($v1) * $_($v2) = ", $v1 * $v2, "\n"; } } while (@zip);
    A(4) * B(5) = 20 A(4) * C(10) = 40 A(4) * D(2) = 8 A(4) * E(9) = 36 B(5) * C(10) = 50 B(5) * D(2) = 10 B(5) * E(9) = 45 C(10) * D(2) = 20 C(10) * E(9) = 90 D(2) * E(9) = 18
Re: iterations in a hash
by Laurent_R (Monsignor) on Aug 04, 2013 at 19:12 UTC

    i want to multiply the A with the B , then the A with the C .. A with D .. A with E.. and then B with A and so on.. i have been trying to compute that without success

    If you do that, you'll end up with all values of the hash being the product 4*5*10*2*9. I doubt that's what you want. Or, probably I did not really understand what you exactly want. Or perhaps you want to end up with a two dimentional table? Please explain.

Re: iterations in a hash
by kcott (Abbot) on Aug 05, 2013 at 09:10 UTC

    G'day madM,

    Welcome to the monastery.

    As others have pointed out, this is a poorly worded question: you tell from all the guesswork and requests for more information. Better questions get better answers: "How do I post a question effectively?" explains how to achieve this.

    Here's my take on a solution:

    $ perl -Mstrict -Mwarnings -E ' my %hash = (A => 4, B => 5, C => 10, D => 2, E => 9); local $" = "\t"; for my $key (sort keys %hash) { say "@{[map { qq{$key * $_ = } . $hash{$key} * $hash{$_} } sort grep { $_ ne $key } keys %hash]}" } ' A * B = 20 A * C = 40 A * D = 8 A * E = 36 B * A = 20 B * C = 50 B * D = 10 B * E = 45 C * A = 40 C * B = 50 C * D = 20 C * E = 90 D * A = 8 D * B = 10 D * C = 20 D * E = 18 E * A = 36 E * B = 45 E * C = 90 E * D = 18

    -- Ken

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (17)
As of 2015-07-28 19:23 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 (258 votes), past polls