Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Sunday 28 April 2019

IOPP Program No.7.B

Program No.7.B 

Write a Perl script to find the highest marks of a subject. The marks of different subject along with the names of the students are maintained in a “result.txt” file.

Solution:

#!/usr/bin/perl

$filename="result.txt";
open(filedesc,$filename) or die "File can't be opened.";

$maxphy=0;
$maxchem=0;
$maxmath=0;

while(<filedesc>)
{
        @fields=split(':',$_);

        if($fields[1]>$maxphy) {
                $maxphy=$fields[1];
                $namephy=$fields[0];
        }

        if($fields[2]>$maxchem) {
                $maxchem=$fields[2];
                $namechem=$fields[0];
        }

        if($fields[3]>$maxmath) {
                $maxmath=$fields[3];
                $namemath=$fields[0];
        }
}
print"Physics max marks : $maxphy, Name : $namephy\n";
print"Chemistry max marks : $maxchem, Name : $namechem\n";

print"Maths max marks : $maxmath Name : $namemath\n";

0 comments:

Post a Comment