Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Adding numbers from a loop

by BillKSmith (Monsignor)
on Aug 07, 2019 at 14:23 UTC ( [id://11104099]=note: print w/replies, xml ) Need Help??


in reply to Adding numbers from a loop

There is nothing wrong with your code. All you really have to add total_revenue processing is to declare $total_revenue outside the loop and add to it inside the loop. However, you can simplify the logic by using stevieb's hash suggestion.
>type htmanning2.pl use strict; use warnings; my @hash_rows = ( {total => 3, other_col => 'fie'}, {total => 6, other_col => 'fum'}, {total => 5, other_col => 'oop'}, ); my %revenues = ( 3 => 30, 6 => 48, 10 => 50, ); my $total_revenue; foreach my $pointer (@hash_rows) { # simulate fetchrow_hashref for po +st my $total = $pointer->{total}; next if (!defined $total or !exists $revenues{$total}); my $revenue = $revenues{$total}; $total_revenue += $revenue; print "revenue: $revenue\ntotal_revenue $total_revenue\n\n"; } >perl htmanning2.pl revenue: 30 total_revenue 30 revenue: 48 total_revenue 78 >
Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11104099]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-25 20:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found