use strict; use Fcntl ':flock'; my $file = "inputdata.txt"; open(FH, $file) or die "File Not Found: $!"; flock(FH, LOCK_EX); my @students; my %hash; my @mul = (0.2,0.25,0.25,0.3); while () { my ($student, @grades) = (split ' ', $_); # split at one or more whitespaces my $grade = 0; for (0..$#mul) { $grade += $mul[$_] * $grades[$_] } $hash{$student} = $grade; # save the sum as a value to the student name } foreach my $key(sort {$hash{$b} <=> $hash{$a}} keys %hash){ print "$key: $hash{$key}\n"; } close(INFILE);