Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: New to coding - Looping code to save time

by duelafn (Vicar)
on May 11, 2012 at 15:50 UTC ( #970047=note: print w/replies, xml ) Need Help??


in reply to New to coding - Looping code to save time

Something like:

## Note: INDEX STARTS AT 0, I show day number here for convenience: ## Day: 1, 2, 3, 4, 5, ... my @start = ( 7, 6, 7, 0, 9, ... ); my @stop = (15, 16, 18, 0, 12, ... ); my @personal = ( 1, 1, 0, 0, 1.5, ... ); my (@daily, @busi, $daily_total, $busi_total, $pers_total); # array indices start at zero: for my $i (0..30) { next unless $stop[$i] > 0; $daily[$i] = $stop[$i] - $start[$i]; $busi[$i] = $daily[$i] - $personal[$i]; $daily_total += $daily[$i]; $busi_total += $busi[$i]; $pers_total += $personal[$i]; }

Update: Ninja'd by kennethk - oh well. Note that my solution will save the daily totals while kennethk's solution uses temporary variables. Use whichever is appropriate for your situation. (and do read the references provided by kennethk).

Good Day,
    Dean

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://970047]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2017-06-22 19:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (528 votes). Check out past polls.