Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

New to coding - Looping code to save time

by BluGeni (Novice)
on May 11, 2012 at 14:47 UTC ( #970034=perlquestion: print w/replies, xml ) Need Help??
BluGeni has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to create a loop that would go to 31 instances so i dont have to write the code every time. i am new to coding and trying to learn it. here is what i have now and i just need to know how to code it so it is just a loop
my $daily1=$stop1-$start1; my $busi1=$daily1-$personal1; $dailytotal+=$daily1; $busitotal+=$busi1; $perstotal+=$personal1; if ($stop2 > 0) { $daily2=$stop2-$start2; $busi2=$daily2-$personal2; $dailytotal+=$daily2; $busitotal+=$busi2; $perstotal+=$personal2; } if ($stop3 > 0) { $daily3=$stop3-$start3; $busi3=$daily3-$personal3; $dailytotal+=$daily3; $busitotal+=$busi3; $perstotal+=$personal3; }
i would rather not do this till 31 any help would be greatly appreciated, thank you!

Replies are listed 'Best First'.
Re: New to coding - Looping code to save time
by kennethk (Abbot) on May 11, 2012 at 15:41 UTC
    What materials are you using to learn? has a number of resources available, including free and open access to Beginning Perl by Simon Cozens, and chapter 3 is particularly on point here.

    You probably want to store your stop, start, and personal data in arrays (see perldata for a description), which you can then cycle over using Foreach Loops, though for this case it might more sense to use C-style For Loops or while loops. Your code might end up looking something like:

    my $daily_total = 0; my $busi_total = 0; my $pers_total = 0; for my $i (0 .. 30) { if ($stop[$i] > 0) { my $daily = $stop[$i] - $start[$i]; my $busi = $daily - $personal[$i]; $daily_total += $daily; $busi_total += $busi; $pers_total += $personal[$i]; } }

    Of course, this depends on your ultimate spec - what you take in, what you output, etc.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: New to coding - Looping code to save time
by sauoq (Abbot) on May 11, 2012 at 15:04 UTC
    i just need to know how to code it so it is just a loop

    In order to do that right, you should also learn something about arrays (and maybe more complex data structures.)

    Where are your $dailyN, $stopN, $startN, $busiN, and $personalN variables coming from?

    "My two cents aren't worth a dime.";
      they are variables that I have set and defined earlier in the program.
Re: New to coding - Looping code to save time
by duelafn (Vicar) on May 11, 2012 at 15:50 UTC

    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,

Re: New to coding - Looping code to save time
by BillKSmith (Vicar) on May 11, 2012 at 20:31 UTC

    You could do exactly what you ask with "symbolic reference". I strongly recommend that you do not use it. Learn to use arrays or hashes instead.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://970034]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2018-06-24 05:21 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.