Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

$ last -wi | grep \"Oct 24\" | tail -10 > signindata fs264fa160116 pts/2 10.10.7

ID: 3816461 • Letter: #

Question

$ last -wi | grep "Oct 24" | tail -10 > signindata

fs264fa160116 pts/2 10.10.76.222 Mon Oct 24 17:16 - 18:00 (00:43)
cfs264fa160119 pts/0 204.77.41.129 Mon Oct 24 16:27 - 17:50 (01:22)
cfs264fa160120 pts/4 73.94.110.23 Mon Oct 24 13:08 - 17:53 (04:44)
cfs264fa160113 pts/3 73.37.142.145 Mon Oct 24 13:07 - 13:42 (00:34)
cfs264fa160110 pts/2 73.94.120.153 Mon Oct 24 12:52 - 15:22 (02:30)
cfs264fa1607 pts/4 50.225.90.17 Mon Oct 24 11:43 - 12:27 (00:44)
cfs264fa160108 pts/3 69.180.183.31 Mon Oct 24 10:36 - 12:14 (01:38)
cfs264fa160110 pts/0 73.94.121.91 Mon Oct 24 10:34 - 13:58 (03:23)
cfs264fa160110 pts/2 73.94.123.63 Mon Oct 24 09:23 - 12:24 (03:01)
ics499fa160124 pts/2 69.180.183.222 Mon Oct 24 07:29 - 08:08 (00:39) $

1) Modify the Perl program lab10e.pl so that it prints the total session time used by all users in the file
The code should be executable as below.
$ ./lab10p1.pl signindata

$ cat lab10e.pl
#!/usr/bin/perl -w
open(FILE, $ARGV[0]) or die("Could not open the file $ARGV[0]");
$sum = 0;
while ($line = <FILE>) {
if ($line =~ /$ARGV[1]/) {
($beginning, $time) = split('(', $line);
($hour, $min) = split(':', $time);
($min) = split(')', $min);
$sum = $hour * 60 + $min + $sum;
}
}
print "Total time = ", int($sum / 60), " hours and ", $sum % 60, " minutes ";
close(FILE);

Explanation / Answer

#!/usr/bin/perl -w
open(FILE, $ARGV[0]) or die("Could not open the file $ARGV[0]");
$max_time = -1;
$max_id = "";
while ($line = <FILE>){
($user_id, $line) = split(' ', $line, 2);
(undef, $time) = split('(', $line);
($hour, $min) = split(':', $time);
($min) = split(')', $min);
$time = $hour*60 + $min;
if ($time > $max_time)
{
$max_time = $time;
$max_id = $user_id;
}
}

print "User id with maximum session length ", $max_id, " ";
close(FILE);

Sample run

$ perl -w lab10e.pl signindata
User id with maximum session length cfs264fa160120

Please rate positively if this answered your query.