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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

There are a few things I would do differently

  • I'd put the most of the variables into the loop, restricting the scope to just the places they are used will spare you some grief later.
  • You need to test that your regular expression matched, if it doesn't you end up processing the previous record again.
  • If you have a student with the surname O'Hara you will fail to process her records.
  • $status has the value of the last mark checked, I think you mean to check if any failed
  • The use of . to join the strings is ugly, use variable interpolation instead.

Anyway this is my version, it still fails to report marks for Ms. O'Hara but at least you will get a warning, and I have to leave something for you;-) The other change is to not explicitly code the number of tests, this will work if the input file has results of four tests.

#!/net/perl/5.10.0/bin/perl use strict; use warnings; use List::Util qw(sum); my $input_file = 'marks.txt'; open my $DATA, '<', $input_file or die "Can't open $input_file: $!\n"; while ( my $line = <$DATA> ) { next if $line =~ /^NAME/; chomp $line; if ( $line =~ /^\w+,\w+(?:,\d+)+$/ ) { my ( $first_name, $last_name, @marks ) = split ',', $line; my $totalmarks = sum @marks; my $average = sprintf "%.2f", $totalmarks / @marks; my $fail = 0; foreach my $mark (@marks) { $fail ||= checkfail($mark); } if ( !$fail ) { print "$first_name $last_name\t@marks \t$totalmarks\t$aver +age\n"; } else { print "$first_name $last_name\t@marks \t\n"; } } else { warn "Invalid input: $line\n"; } } close $DATA or die "Can't close $input_file: $!\n"; #To check whether the student failed sub checkfail { my $marks = shift; return $marks < 35; } john michael 100 50 60 210 70.00 sam shane 50 60 70 180 60.00 tom christo 30 40 50

You may wish to give consideration to using Text::CSV_XS for parsing comma delimited text files.

Update: two small code modifications kindly suggested by moritz.


In reply to Re: Students Rank calculation for the below perl script by hipowls
in thread Students Rank calculation for the below perl script by valavanp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-18 18:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found