Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

simultaneously adding and multiplying numbers in a hash/ array

by derpp (Acolyte)
on Aug 09, 2010 at 21:13 UTC ( #853917=perlquestion: print w/ replies, xml ) Need Help??
derpp has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys,

I'm kind of stuck on a stupid question. I need to multiply a number by another number, then add it to the previous number..numerous times.

Here's a made up version of my code, since I'm extracting the data I'm using from MS excel and it would take way too long to type all that up.

use warnings; my @change = (1.15, -0.1, 5.4, 1.03, -0.241); my @numberofshares = (100, 400, 200, 300, 240); @hash{@change} = @numberofshares; while (($k, $v) = each %hash) { foreach ($v1) { $product = $v1*$k1 } }

Yes, I know this gives the incorrect number, but what I want to do is after each $k*$v, i'd add it to the previous result of $k*$v, and on and on.

I'm only a beginner at this, so please excuse my incorrect spacing or whatever in my code. Thanks in advance!

Comment on simultaneously adding and multiplying numbers in a hash/ array
Download Code
Replies are listed 'Best First'.
Re: simultaneously adding and multiplying numbers in a hash/ array
by toolic (Bishop) on Aug 09, 2010 at 21:20 UTC
      Thanks! I can't believe the answer was so simple..
Re: simultaneously adding and multiplying numbers in a hash/ array
by ikegami (Pope) on Aug 09, 2010 at 21:44 UTC
    The hash is neither required nor helpful.
    my @change = (1.15, -0.1, 5.4, 1.03, -0.241); my @numberofshares = (100, 400, 200, 300, 240); my $product = 0; for (0..$#change) { $product += $change[$_] * $numberofshares[$_]; }
Re: simultaneously adding and multiplying numbers in a hash/ array
by AnomalousMonk (Abbot) on Aug 09, 2010 at 22:12 UTC

    The use of a hash in this application is positively dangerous unless the elements of the  @change array (which become the keys of the hash) can be guaranteed to be unique. E.g.:

    >perl -wMstrict -le "my @change = (1.15, -0.1, 5.4, 1.03, 1.15, -0.241); my @numberofshares = (9999, 400, 200, 300, 500, 240); my %hash; @hash{@change} = @numberofshares; printf qq{%d keys in hash (oops...) \n}, scalar keys %hash; my $sum = 0; while (my($k, $v) = each %hash) { $sum += $v * $k; } print qq{sum == $sum (?)}; " 5 keys in hash (oops...) sum == 1866.16 (?)

    Update: The algorithm can be expressed more concisely with some list-processing functions:

    >perl -wMstrict -le "use List::Util qw(sum); use List::MoreUtils qw(pairwise); use vars qw($a $b); my @change = (1.15, -0.1, 5.4, 1.03, -0.241); my @numberofshares = ( 100, 400, 200, 300, 240); my $sum = sum pairwise { $a * $b } @change, @numberofshares; print qq{sum == $sum}; " sum == 1406.16

    I think it could be expressed yet more concisely in Perl 6, but my 6-fu is not that great. Would it be something like:
        my $sum +=<<*<< @change, @numberofshares;

      >> I think it could be expressed yet more concisely in Perl 6

      I like:

      say [+] @change <<*>> @numberofshares
Re: simultaneously adding and multiplying numbers in a hash/ array
by psini (Deacon) on Aug 09, 2010 at 21:22 UTC

    Not sure I've understand the problem. You want the sum of the products of a set of pairs, right?

    If so, you don't need a hash, you could do:

    use strict; use warnings; my @change = (1.15, -0.1, 5.4, 1.03, -0.241); my @numberofshares = (100, 400, 200, 300, 240); my $sum=0; while (my $k=pop @change) { $sum+=$k*pop @numberofshares; } print $sum;

    Warning: untested

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

Re: simultaneously adding and multiplying numbers in a hash/ array
by JavaFan (Canon) on Aug 10, 2010 at 12:18 UTC
    my $sum = 0; for (my $i = 0; $i < @change; $i++) { $sum += $change[$i] * $numberofshares[$i]; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (16)
As of 2015-07-31 13:45 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 (278 votes), past polls