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

A little problem

by yabba
on Apr 08, 2001 at 18:49 UTC ( #70837=perlquestion: print w/replies, xml ) Need Help??

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 18: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
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: A little problem
by Beatnik (Parson) on Apr 08, 2001 at 19: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
Node Status?
node history
Node Type: perlquestion [id://70837]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2020-11-30 11:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?