Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Programming Perl 3rd ed. Chapter 1

by blacksmith (Hermit)
on Jun 23, 2001 at 01:19 UTC ( [id://90872]=perlquestion: print w/replies, xml ) Need Help??

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

This is for all of you who have read the Programming Perl book. In trying to pick the code apart before proceeding with the text I came across this. I understand some VERY BASICs of programming, and I am just beginning this book in hope that I will be able to get more in depth and be able to effectively write codes in half the space in which I have written them before (ie. Document Maintenance Script). The first example in the book :
#!/usr/bin/perl open(GRADES, "grades") or die "Can't open grades: $!\n"; while ($line = <GRADES>) { ($student, $grade) = split(" ", $line); $grades{$student} .= $grade . " "; } foreach $student (sort keys %grades) { $scores = 0; $total = 0; @grades = split(" ", $grades{$student}); foreach $grade (@grades) { $total += $grade; $scores++; } $average = $total / $scores; print "$student: $grades{$student}\tAverage: $average\n"; }
has me wondering. I have created the file that the book lists with the names and grades and I get a message, "Illegal division by zero at grades.pl line 17, <GRADES> line 10". Now I understand if I am not allowed to divide by 0 since that would give me 0. What I was wondering was this script was written correctly for the example they are making. Thanks.
Blacksmith

Replies are listed 'Best First'.
(ichimunki) Re: Programming Perl 3rd ed. Chapter 1
by ichimunki (Priest) on Jun 23, 2001 at 01:43 UTC
    The only way you are going to have a division by zero error is if there are no elements in @grades for a given $student. Simple way to debug is to add a print after @grades = split... that goes something like
    print "student: $student :: grades: " . $grades{$student} . "\n"; or print "student: $student :: grades: " . join( ' ', @grades ) . "\n";

    and see if any of the data looks inappropriate.
      Thanks guys. I think I found out what happened. The code had a space between the quotes in the 12th line. I took that space out and the program ran without an error message. The students got some really poor scores. HAHAHA. Thanks for the input. I think that got it.
      Blacksmith.
Re: Programming Perl 3rd ed. Chapter 1
by converter (Priest) on Jun 23, 2001 at 01:39 UTC

    $scores begins with a value of zero, and is incremented in the foreach loop, which iterates over the @grades array. Chances are, your split() statement is returning an empty list leaving @grades empty and nothing for the foreach loop to do, so the value of $scores remains at zero and causes the error in $average = $total / $scores.

Re (tilly) 1: Programming Perl 3rd ed. Chapter 1
by tilly (Archbishop) on Jun 23, 2001 at 07:42 UTC
    If edition 3 is like edition 2 was, then I advise you to read their disclaimer about limitations of the program and then look very carefully at the list of students and grades that they have.

    The hidden moral is the old saying, "Don't make an ASS out of U and ME."

    There are a lot of gems like that in the book. That one sticks out in my memory because when I was first learning Perl I skimmed the first chapter, went back and began reading more carefully. I got to that, tried to figure out the example, and did. I read their comment, figured out why it was true, smirked at the pun, and then read the input file. Again.

    That was when I decided that whoever wrote this book was rather different than the authors of the VB garbage I had been dealing with. These people had my kind of humor...

Re: Programming Perl 3rd ed. Chapter 1
by tigervamp (Friar) on Jun 23, 2001 at 19:56 UTC
    BTW,

    Dividing by 0 does not yield 0, but an undefined value, which is why it is illegal.

    Example:
    7*0=0
    therefore, logic yields that 7=0/0

    you can do the same for any number, obviosuly 0/0 cannot be defined, and neither can x/0.

    tigervamp

Log In?
Username:
Password:

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

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

    No recent polls found