#!/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$average\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