You have not said whether the other number is in the database or elsewhere in your code.

I assume not in the DB, or you'd just multiply the values in the columns: $sum += ($record->{total} * $record->{price});.

If from another source, and you need to look it up, say from a price sheet based on quantity ordered, make yourself a lookup table:

use strict; use warnings; use feature 'state', 'say'; use Test::More; use JSON; sub calc_rev { state %price_by_qty = ( 1 => 15, 3 => 10, 6 => 8, 10 => 5, ); my $qty = shift; return 0 if ! $qty; for my $level ( sort { $b <=> $a } keys %price_by_qty ) { return $qty * $price_by_qty{ $level } if $qty >= $level; } } # # mock getting an aref of rows for this test file my $json = do { local $/; <DATA> }; my $rows = decode_json( $json ); # my $revenue = 0; for my $row ( @{ $rows } ) { $revenue += calc_rev($row->{total}); } is( $revenue, 123, 'Revenue is 123. Mwahahahaa!'); done_testing; __DATA__ [ {"total":3}, {"total":6}, {"total":1}, {"total":2} ]
Output:
$ perl foo.pl ok 1 - Revenue is 123. Mwahahahaa! 1..1

Hope this helps!


The way forward always starts with a minimal test.

In reply to Re^3: Adding numbers from a loop by 1nickt
in thread Adding numbers from a loop by htmanning

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.