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

I just wanted to say that I'm a new PERL programmer and I have lots of things to learn. I've been working on this program for some time. I'm having trouble with opening and closing and writing to my file. Here's what I got so far:
use strict; my @data; my $avg; my $dev; my $std; my $tmp; my $grade; my $line; open(IN, "pa5c.dat") || die ("Can't open file $!\n"); while($line = <IN>){ chomp ($line); my $tmp = [split /:/]; $avg += $tmp->[1]; push @data, $tmp; } $avg = scalar(@data) ? $avg /= @data : 0; my $std = 0; for (@data) { $std += abs($_->[1] - $avg) } $std = scalar(@data) ? $std /= @data : 0; 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; __END__#pa52.pl
The pa5c.dat file contains:
Smith:70 Jones:74 Rider:80 High:82 Billie:68 Smithsonville:76
Now if my program runs correctly, my program should assign letter grades to the students.

Replies are listed 'Best First'.
(jptxs) Re: Filehandles - opening/closing a filehandle
by jptxs (Curate) on Apr 08, 2001 at 20:02 UTC
    could not in good conscience answer this in full for I once was a teacher, too. so let me do two things to point you in the right way.
    • you're on the right track. refine what you have and it will work. break everything down to distinct actions. literally, speak your program out as a paragraph, then break the code up to match the sentances. test each piece on it's own and as they work put them together.
    • look up Elements of Programming with Perl at your local library/bookstore. don't buy it if you don't want, but it has an example almost exactly like this assignment you could model your program from.
    Hope that helps.
    "A man's maturity -- consists in having found again the seriousness one had as a child, at play." --Nietzsche
Re: Filehandles - opening/closing a filehandle
by rchiav (Deacon) on Apr 08, 2001 at 20:21 UTC
    This isn't a response to help you figure out what's going on with your script since I think the previous answers have taken care of that. I just wanted to give you some advice on the questions that you post. It would be to your benifit to try to structure them a little better. For one, you haven't told anyone what happens when you run this. There's actually no question.

    If you'd provide output, or an error, or what's happening that's unexpected, I think people would be more willing to help. I'd also suggest reading this thread. It has a lot of really good information on how to post good questions.

    I think you'll find that the people here are some of the most helpful you'll find anywhere, but only if you post appropriately. A little more thought to your questions will go a long way.

    Good luck with the program...
    Rich
    bash: undo: command not found

Re: Filehandles - opening/closing a filehandle
by wardk (Deacon) on Apr 08, 2001 at 20:02 UTC

    wrt files, you don't appear to be closing your file before opening it again for write.