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

Re^2: Programming Perl 3rd ed. Chapter 1

by Anonymous Monk
on Jan 12, 2026 at 23:36 UTC ( [id://11167145]=note: print w/replies, xml ) Need Help??


in reply to Re: Programming Perl 3rd ed. Chapter 1
in thread Programming Perl 3rd ed. Chapter 1

What you did will help prevent typos, but now you're using lexical variables as globals which is not a good idea. You should declare lexicals so they stay inside the relevant scope. Also got rid of double quotes where interpolation is not needed, and lost any unneccessary parenthesis, and specify the mode for open:
open my $fh, '<', 'grades' or die "Can't open grades: $!\n"; while (my $line = <$fh>) { my ($student, $grade) = split ' ', $line; $grades{$student} .= $grade . ' '; } foreach my $student (sort keys %grades) { my ($scores, $total) = (0, 0); my @grades = split ' ', $grades{$student}; foreach my $grade (@grades) { $total += $grade; $scores++; } my $average = $total / $scores; print "$student: $grades{$student}\tAverage: $average\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11167145]
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 2026-04-21 14:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    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.