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

Averages in bowling

by Andrew_Levenson (Hermit)
on Mar 21, 2006 at 04:48 UTC ( [id://538127]=perlquestion: print w/replies, xml ) Need Help??

Andrew_Levenson has asked for the wisdom of the Perl Monks concerning the following question:

I'm redoing my bowling averages program using multidimensional arrays to make it shorter. However, I can't get this one section to work out. I can't figure out how to express it. $k is supposed to have 3 values, $j is supposed to have 3 values, and $i is supposed to have 4 values. But... I can't get the loop to work with $i in there, but it's integral to the equation.
until($k==2){ @averages[$k]=int((@scores[$i][$j]+@scores[$i][$j+1]+@scores[$i][$ +j+2]+@scores[$i][$j+3])/3); $i++; $j++; $k++; }
Any suggestions? Thanks.

Replies are listed 'Best First'.
Re: Averages in bowling
by ikegami (Patriarch) on Mar 21, 2006 at 05:21 UTC

    Where to start!

    • $k isn't assigned an initial value.
    • until (...) { ... } is not Perl. It doesn't work.
    • And if it did, it would only loops twice (once with $k=0 and once with $k=1) when I think you also want it to loop with $k=2.
    • @array[$index] should be $array[$index]. The sigil to use is of the type returned.
    • $i and $k always have the same value.
    • $j is ever increasing, yet you keep adding an offset to it in a way that makes no sense.

    The first three points are fixed by using for my $k (0..2) { ... }.

    I can't even guess at what you are trying to do. Could you answer the following?

    • averages is an array of what? "averages for each _____" (fill in the blank)
    • scores is an array of what? "scores for each _____ for each ____" (fill in the blanks)
      until (...) { ... } is not Perl. It doesn't work.

      Ahem :)

      [0] Perl> until( $i == 5 ) { print $i++; };; Use of uninitialized value in numeric eq (==) at 0 1 2 3 4

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Whoa! That's weird! I did run a test before making that statment. I don't know why, but my test (below) freezes (while yours works). A bug in the optimization code?

        perl -e "until (0) { print "a\n" }

        (I figured I'd just Ctrl-C it)

      I can't give the entire program now because I do not have it on hand, but for now:

      In the program, each value is initialized at 0.
      Averages is an array of averages of the scores from the array @scores.
      Scores is a multidimensional array consisting of arrays of bowlers, each with an array of scores. Scores are entered by the user.

      The main problem is that there are 4 bowlers, but only 3 games, so it makes looping while raising values a little difficult.
        So the average is by bowler? Then you only need two counter variables, not three. If you'd give your vars more meaningful names, it would go much easier. I'm going to try to do this intructively.
        1. What is it you want to do? You want to calculate the average score of every bowler.

        2. That means you need to loop for every bowler:

          for my $bowler (0..3) { ... }

          Better yet, let's not assume we know how many games there are:

          for my $bowler (0..$#scores) { ... }

          We will be working with the scores of a specific bowler, so let's create a shortcut:

          for my $bowler (0..$#scores) { my $bscores = $scores[$bowler]; ... }
        3. Now, we need to calculate the average score of a bowler:

          $averages[$bowler] = int(($bscores->[0] + $bscores->[1] + $bscores->[2])/3);

          That's ok, but it could be better. We could use a loop to remove the redundancy and to avoid assuming we know how many games there are:

          my $sum = 0; for my $game (0..$#$bscores}) { $sum += $bscores->[$game]; } $averages[$bowler] = int($sum/@$bscores);

          We can clean it up a bit by using a foreach loop (since $game is only used as an index into @$bscores):

          my $sum = 0; $sum += $_ foreach @$bscores; $averages[$bowler] = int($sum/@$bscores);
        4. All together now:

          for my $bowler (0..$#scores) { my $bscores = $scores[$bowler]; my $sum = 0; $sum += $_ foreach @$bscores; $averages[$bowler] = int($sum/@$bscores); }
        5. You could even use List::Util to do the sum:

          use List::Util qw( sum ); for my $bowler (0..$#scores) { my $bscores = $scores[$bowler]; $averages[$bowler] = int(sum(@$bscores)/@$bscores); }

          (Why isn't that module part of core?)

Re: Averages in bowling
by MCS (Monk) on Mar 21, 2006 at 13:13 UTC

    Well first, when dealing with a point in an array, you need to use $, not @. ie:

    until ($k==2) { $averages[$k]=int(($scores[$i][$j]+$scores[$i][$j+1]+$scores[$ +i][$j+2]+$scores[$i][$j+3])/3); $i++; $j++; $k++; }

    The above runs fine for me, granted I have no data and no idea what $i, $j and $k mean (though I can guess). So if the above doesn't help, we'll need more information, namely the values for $i, $j, $k up to this point and the expected output.

    *Edit: I thought I would take a second and look at what you had and fill in some default values and unless you have major parts of this loop excluded for whatever reason, what you are doing doesn't make sense. First, $i, $j and $k are all set to the same values. You probably want nested for loops or something and if you provide more information on how your arrays are setup, we can help you more.

Re: Averages in bowling
by graq (Curate) on Mar 21, 2006 at 17:01 UTC
    I am still perplexed as to what you actually are trying to do. So, I have modified your code to the following:
    #!perl use strict; use warnings; my( $k, $i, $j ) = ( 0, 0, 0 ); until($k==2){ # @averages[$k]=int((@scores[$i][$j]+@scores[$i][$j+1]+@scores[$i][$ +j+2]+@scores[$i][$j+3])/3); print "AVERAGE [$k] = (scores[$i][$j] + scores[$i][".($j+1); print "] + scores[$i][".($j+2); print "] + scores[$i][".($j+3); print "]) /3)\n"; $i++; $j++; $k++; }

    Now, it isn't very pretty, but it takes your snippet, makes it work, and produces some debug info to match against your desired result.

    > perl test.pl AVERAGE [0] = (scores[0][0] + scores[0][1] + scores[0][2] + scores[0][ +3]) /3) AVERAGE [1] = (scores[1][1] + scores[1][2] + scores[1][3] + scores[1][ +4]) /3)

    Is this really what you wanted?

    -=( Graq )=-

Log In?
Username:
Password:

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

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

    No recent polls found