COMP111: Unix and Script Programming: The Hong Kong University of Science & Technology
COMP111: Unix and Script Programming: The Hong Kong University of Science & Technology
COMP111: Unix and Script Programming: The Hong Kong University of Science & Technology
Instructions:
Problem 1 ______________/ 11
Problem 2 ______________/ 5
Problem 3 ______________/ 5
Problem 4 ______________/ 9
Problem 5 ______________/ 10
Problem 6 ______________/ 18
Problem 7 ______________/ 10
Problem 8 ______________/ 11
Problem 9 ______________/ 6
Problem 10 ______________/ 10
Problem 11 ______________/ 5
#!/usr/local/bin/perl5 w
@b{"one","two",three} = qw(three two one);
print $b{one}\n;
b) Fill in the blank so that the program lists out all the key-value pairs in %b (2
marks)
#!/usr/local/bin/perl5 w
$b{a} = b;
$b{111} = a;
foreach __________________________________________ {
print key: $i, value: $b{$i}\n;
}
Answer: foreach ($i (keys(%b)) (0 or 2 marks)
c) Fill in the blank so that the program lists out all the key-value pairs in %b (2
marks)
#!/usr/local/bin/perl5 w
$b{a} = b
$b(111) = a
while _____________________________________ {
print key: $i, value: $k \n;
}
Answer: while (($i,$k) = each(%b)) (0 or 2 marks)
d) Fill in the blank so that the program lists out all the key values in %b (2
marks)
P.S. you can only use at most one @b, and you cant use any $b and %b in your
answer.
#!/usr/local/bin/perl5 w
$b{a} = b
$b(b) = a
print The values are: ____________________________ \n;
e) Fill in ONE WORD/VARIABLE only for each of the three blanks, so that the
hash %b ends up with 2 key-value pairs only where the keys are Fruit and
Fruits. (3 marks)
#!/usr/local/bin/perl5 -w
$b{"Apple"} = "Fruit";
$b{Oranges} = FruitFruit;
$b{"Orange"} = "Fruit";
$ cat perlio1
#!/usr/local/bin/perl5 -w
@lines = <STDIN>;
@a = ();
$a = @lines;
print "$a\n";
$ perlio1
aaa
bbb
ccc
<CRTL-D>
Answer:
3
(b) Which of the following commands can correctly execute the following Perl program? (3
marks)
$ cat perlio2
#!/usr/local/bin/perl5 -w
while (<>)
{
print;
}
$ cat input
aaa
bbb
ccc
A) perlio2 input
B) perlio2 < input
C) cat input | perlio2
D) All of the above
Answer:
D
a.o b.o
abc
The following are some standard output,
$ make
g++ -c a.cpp
g++ -c b.cpp
g++ -o abc a.o b.o
$ make
make: 'abc' is up to date.
$ make clean
rm *.o
$ ls
abc a.cpp a.h b.cpp b.h c.h Makefile
Please complete the Makefile. Use <TAB> to indicate a TAB character. No partial marks will
be given for each answer for this question. (5 marks)
Answer:
___________________________
g++ -o abc a.o b.o
___________________________
g++ -c a.cpp
___________________________
g++ -c b.cpp
___________________________
Answer:
abc: a.o b.o----(1)
g++ -o abc a.o b.o
Answer:
It forces all variables to require declaration with my() (i.e., it makes them all
local variables) (0 or 2 marks)
Answer:
All variables are local, so we will never reference wrong variable (belonging to
a different scope) because of typo. (0 or 2 marks)
#!/usr/local/bin/perl5 -w
$a = 1;
$b = 5;
$max = max($a, $b, 2);
print "max: $max\n";
$max = max($b, $b, 2, $b, 7, $b, 6);
print "max: $max\n";
_________________________{
$max = 0;
foreach ________________________{
if(______________________________){
______________________________;
}
}
return $max;
}
Answer:
$a = 1;
$b = 2;
$max = max($a, $b, 5);
print "max: $max\n";
$max = max($a, $b, 5, 6, 7);
print "max: $max\n";
sub max{ (0 or 1 mark)
$max = 0;
foreach $n (@_){ (0 or 2 marks)
if($n > $max){ (0 or 2 marks)
$max = $n;
}
}
Normal Case:
$ cat input
aaa
bbb
ccc
$ perlfile1 input output
$ perlfile1 input output
$ cat output
aaabbbcccc
Error Case:
$ perlfile1 input1 output
Error opening file input1:No such file or directory at
./perlfile1 line 6.
(Note: The program displays the error message Error opening
file input1, OS error message No such file or directory, and
the line the error occurs at ./perlfile1 line 6)
$ perlfile1 input output
Error opening file output
(Note: The program display error message Error opening file
output)
#!/usr/local/bin/perl5 -w
$infile = ____________________________________________;
$outfile = ____________________________________________;
unless(open(IN, ________________))
{
die ____________________________________________;
}
unless(open(OUT, ________________))
{
die ____________________________________________;
}
____________________________________________;
print OUT @lines;
close(IN);
close(OUT);
Answer:
$infile = $ARGV[0]; ----(1 mark)
$outfile = $ARGV[1]; ----(1 mark)
unless(open(IN, $infile)) ----(1 mark)
die "Error opening file $infile:$!"; ----(2 marks)
unless(open(OUT, ">$outfile")) ----(1 mark)
die "Error opening file $outfile\n"; ----(1 marks)
chomp(@lines = <IN>); ----(1 marks)
$ perlfile2 dir
dir is a directory
$ perlfile2 /dev/null
/dev/null is not a plain file
$ perlfile2 input
aaa
bbb
ccc
$ cat input
aaa
bbb
ccc
#!/usr/local/bin/perl5 -w
$infile = $ARGV[0];
if (_______________________________________________)
{
die "$infile is a directory\n";
}
if (_______________________________________________)
{
die "$infile is not a plain file\n";
}
unless(open(IN, $infile))
{
die "Error opening file $infile:\n";
}
@lines = <IN>;
print @lines;
close(IN);
Answer:
if (-d $infile)----(1 mark)
if (! -f $infile)----(1 mark)
(a) The following program gets a binary number from the standard input, flips the 0s and 1s,
and then outputs to standard output. You may assume the user will only input 1 or 0. The
following is the output of the program (the underlined text is the user input). (4 marks)
$ regexp1
100010001
011101110
$ regexp1
0111100010
1000011101
#!/usr/local/bin/perl5 -w
chomp($line = <STDIN>);
$line =~ ___________________________________________;
$line =~ ___________________________________________;
$line =~ ___________________________________________;
print "$line\n";
Answer:
$line =~ s/1/a/g; ----(1 mark)
$line =~ s/0/1/g; ----(1 mark)
$line =~ s/a/0/g; ----(1 mark)
(Note: Other characters can be used instead of a except 1 and
0)
Please write down the regular expression (without using the &&, ! and || operators) that can
match the pattern described.
(b) 3 numbers which are less than 1000 and they are separated by 2 periods (.). (4 marks)
E.g. 123.456.789 (Match)
1234.344.532 (Not Match)
12.34.67 (Match)
12.445.6789 (Not Match)
Answer: /^\d{1,3}\.\d{1,3}\.\d{1,3}$/
$ regexp3
abcdefxyz
match
$ regexp3
xyzjklabc
match
$ regexp3
abcijk
not match
$ regexp3
xyzttt
not match
Answer:
if ($line =~ /abc/ && $line =~ /xyz/)
$ regexp5
aba
match
$ regexp5
a
match
$ regexp5
abed
not match
$ regexp5
ahji
not match
$ regexp5
%abd%
match
if (_________________________________________________________)
{
print "match\n";
}
else
{
print "not match\n";
}
Answer:
if ($line =~ /^(.).*\1$/ || $line =~ /^.$/)
In this question, you are required to complete a Perl program that can recursively change the
extension of files from .txt to .abc. No partial marks will be given for each answer for this
question. Here are some requirements of the program:
The first argument of the program is the starting directory, and you may assume only
relative paths will be used
From the starting directory, the program should change all the file extensions in sub-
directories as well
For any file operation (including changing directory and renaming files), the program
should exit if any errors occur.
You may assume each file name contains only 1 .. E.g. there will not be any
filenames such as file.txt.txt or file.abc.bak.
$ ls -R test
test:
a admin basic network
test/admin:
df.txt mount.txt rpm.txt special umount.txt
test/admin/special:
grep.txt veryspecial
test/admin/special/veryspecial:
test/basic:
cat.txt cp.txt ls.txt mv.txt rm.txt
test/network:
hostname.txt ifconfig.txt netstat.txt ping.txt route.txt
$ perldir test
$ ls -R test
test:
a admin basic network
test/admin:
df.abc mount.abc rpm.abc special umount.abc
test/admin/special:
grep.abc veryspecial
test/admin/special/veryspecial:
test/basic:
cat.abc cp.abc ls.abc mv.abc rm.abc
test/network:
hostname.abc ifconfig.abc netstat.abc ping.abc route.abc
#!/usr/local/bin/perl5 -w
#
use strict;
rlist($ARGV[0]);
sub rlist
{
my ($path) = @_;
#Change to the working directory
_____________________________________________________;
my @files _____________________________;
foreach my $file (@files)
{
if (-d $file)
{
rlist("$file");
#Change back to parent directory
______________________________________________;
}
if (-f $file)
{
my $oldname = $file;
if (___________________________________________)
{
____________________________________________;
}
}
}
}
my @files = <*>;
if ($file =~ s/\.txt/\.abc/)
{
rename($oldname, $file) || die "cannot
rename\n";
(a) Write a Perl program which stores the result of the date command in a variable $now. [2
marks]
Answer:
$now = `date`; (2 marks)
(b)Write a Perl program which prints the lines containing the string abc in the file 123.txt. [2
marks]
Answer:
system("grep", "abc", "123.txt"); (2 marks)
(c) The following Perl program is supposed to display the first three
lines of each file in the current directory to the screen. What is
wrong? [4 marks]
#!/usr/local/bin/perl5 -w
foreach (<*>) {
open(IN, $_);
open(OUT, >head +3);
@data = <IN>;
print <OUT> @data;
close(IN);
close(OUT);
}
Answer:
open(IN, $_); (1 mark)
open(OUT, "|head -3"); (0, 1 or 2 marks)
print OUT @data; (1 mark)
(d)Fill in the blank so that the program below lists all users who are
logged on. [3 marks]
#!/usr/local/bin/perl5 w
_________________________________________{
($user, $loc, $time) = /(\S+)\s+(\S+)\s+(.*)/;
print "$user logged on $loc, at time $time\n";
}
Answer:
foreach $_ (`who`){
(3 marks for correct answer)
(1 mark only for any answer with $_ and who)
Answer:
<html>
<head>
<title>COMP111 Final Exam QS9</title>
</head>
<body bgcolor="#666666" text="white">
<p align="center"><B><font size = "8">:: HKUST ::</font></B></p>
<p align="center"><img
src="http://www.ust.hk/en/index_img/logo.gif"> </p>
<p align="center"><a href="http://www.ust.hk">Link to HKUST
Homepage</a></p>
<hr>
</body>
</html>
The Perl CGI program below is supposed to implement the game "Paper, Scissors,
Rock", with the user playing against the computer. Fill in the blanks.
(3) You must force all the variables to be declared as local variables.
if($comp eq $guess){
____________________________________;
}elsif((________________________________________________)
||
(________________________________________________)
||
(________________________________________________)){
print p("User wins!");
}else{
print p("Computer wins!");
}
}else{
print start_form();
_________________________________________________________;
_________________________________________________________;
print end_form;
}
print hr;
print end_html();
Answer:
use strict; (0.5 mark)
use CGI qw(:standard); (0.5 mark)
print header(), print start_html(""); (0.5 mark, with end_html)
print h1("Paper, Scissors, Rock"); (0.5 mark)
my $guess = param("choice"); (1 mark)
my $comp = (qw(paper scissors rock))[rand(3)]; (1.5 marks)
if($comp eq $guess){
print p("Draw"); (0 or 1 mark)
}elsif(($guess eq "paper" && $comp eq "rock") || (0 or 2
marks)
($guess eq "scissors" && $comp eq "paper") ||
($guess eq "rock" && $comp eq "scissors")){
#!/usr/local/bin/perl5.00404 -w
use ___________________________ qw(:standard);
_______(-next_page=>__________, -last_page=>________,
-delay=>0.1);
sub next_page {
my($q, $counter) = _______________________________;
my $zero = ______________________________________;
if($counter >= 25) {_______________________________;}
______________________________________________________________;
}
sub last_page {
______________________________________________________________;
Answer:
#!/usr/local/bin/perl5 -w
use CGI::Push qw(:standard); (0.5 mark)
do_push(-next_page=>\&next_page, -last_page=>\&last_page,
-delay=>0.1);
(0.5 mark) (0.5 mark) (0.5 mark)
sub next_page {
my($q, $counter) = @_; (0.5 mark)
my $zero = "0" x $counter; (0.5 mark)
if($counter >= 25){return undef; } (0.5 mark)
return start_html(), $zero, end_html();
}
sub last_page {
return start_html(), DONE, end_html();
}
(ALL start_html and end_html 0.5 mark, zero 0.5 mark, DONE 0.5
mark)