Sunteți pe pagina 1din 2

PERL PROGRAMS

Store the student name and marks of cat2 in osp subject using hash in perl. (i) display the name of the students starts with 'a' and ends with 'r'. (ii) Display the names by removing first and last character. (iii) Display the student names those who have scored greater than class average. #! C:\Perl\bin\perl %student=qw(aashir 100 arnav 100 asad 99 vikrant 95); $length=scalar(%student); $total=0; $avg=0; while(($k,$v)=each(%student)) { print $k."\t"; print $v."\n"; } #display the names which starts with a and ends with r. @stud_keys=keys%student; foreach $a(@stud_keys) { $name=$a; $last=chop($a); $r=reverse($a); $first=chop($r); if(($first eq 'a')&&($last eq 'r')) { print "the name ".$name." starts with v and ends with a\n"; } } #remove the last character of the name and display. @chopped=chop(@stud_keys); print "\nCHOPPED VALUES:"; foreach $a(@stud_keys) { print $a."\n"; } #remove the first character and display. print "\nCHOPPED FIRST CHAR VALUES:"; foreach $a(@stud_keys) { $r=reverse($a);

chop($r); $a=reverse($r); print $a."\n"; } #find out the class average and display the name of the students who have scored more than that. while(($k,$v)=each(%student)) { $total+=$v; } $avg=$total/$length; while(($k,$v)=each(%student)) { if($v>$avg) { print "\n".$k." has scored more than the class average ".$avg." with marks as ".$v."\n"; } }