Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Welcome to the Monastery
 
PerlMonks  

A little problem

by yabba (Initiate)
on Apr 08, 2001 at 14:49 UTC ( [id://70837]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

Ok, this guy really helped me out and here's what he wrote:
my @data; my $avg; while (<IN>) { chomp; my $tmp = [split /:/]; $avg += $tmp->[1]; push @data, $tmp; } $avg /= @data; my $std = 0; for (@data) { $std += abs($_->[1] - $avg) } $std /= @data; open(OUT, ">pa5c.dat") || die ("Can't open file $!\n"); for (@data) { my $grade; my $dev = ($_->[1] - $avg) / $std; if ($dev >= 1) { $grade = 'A' } elsif ($dev >= (1/3)) { $grade = 'B' } elsif ($dev >= (-1/3)) { $grade = 'C' } elsif ($dev > -1) { $grade = 'D' } else { $grade = 'E' } print OUT join(':', $_->[0], $_->[1], $grade) . "\n"; } close OUT; Which produces records like Smith:70:C Jones:74:B Rider:80:A
But now when I run the program, it says: Illegal division by zero at $avg /= @data; Does anyone know whats happening here?

Replies are listed 'Best First'.
(jptxs) Re: A little problem
by jptxs (Curate) on Apr 08, 2001 at 14:59 UTC
    I voted this down, which I hardly ever do. I did it b/c even after being chided for it so many times on your last post you still used such a generic and meaningless title on this one. titles like this just pollute the waters for people who are searching or trying to find something specific in the monastery. that's why people have asked you use titles closer to the meaning of your question. it's not just some random preference some people have - in case you think you're being persecuted by a silly idea. it's just trying to keep the monastery orderly and clean.

    UPDATE: wow. I only just noticed:

    Please, when naming your nodes, think of the content and title them according to that. like:
    • problem getting 3rd line of file
    • $_ not set correctly in for loop
    • use my or local in sub?
    you get the idea. it will really help out if you do.
    "A man's maturity -- consists in having found again the seriousness one had as a child, at play." --Nietzsche
      Can you give me a example using my program. This is like my 4th PERL program and I've been taught this way so I don't know better. I would really appreciated if you can help me, thanks.
        Yes, I can do that.

        But I won't do that.

        And I sincerely hope that nobody else does that either.

        As I told you in a /msg, your use of this site to do your homework is an abuse of this site. In fact if I knew who you were and who your teacher was I would contact your teacher with these threads. Most universities have policies on cheating with punishments up to and including being expelled. I not only would contact the teacher with full knowledge that this might happen, but hoping it would.

        Think back to the story of the difference between giving people fish and teaching them to fish. This site is all about teaching people to fish. You are demanding that we give you fish. If we do, what will you have learned? That if you just abuse all reasonable netiquitte and demand enough, your gaping jaw will have fish shoved in it? I don't want you to learn that. I don't want that to happen. I don't want to see people with a legitimate desire to learn being displaced by beggars looking for handouts.

        So do your own damned homework. If you see something that you don't understand, if you have bugs, etc, then this is a great resource. Use it appropriately. But avoiding doing your homework (which was given to you primarily to help you learn) does not fit what this site is for, and does not fit within the bounds of academic honesty.

        If you use a little common sense then you won't wind up filling Worst Nodes to overflowing and you will get some useful help. If you don't?

Re: A little problem
by Beatnik (Parson) on Apr 08, 2001 at 15:02 UTC
    $avg /= @data; basically means same as $avg = $avg / @data. You're dividing $avg by the number of elements in @data and storing the result back in $avg. The tricky part is that computers have this nasty habbit of freaking out on divisions by zero (because technically you can't divide by zero).

    A fix would include something like :

    $avg = scalar(@data) ? $avg /= @data : 0;

    Beatnik
    ... Quidquid perl dictum sit, altum viditur.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://70837]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.