Q1.

Implement the following instructions in a Perl program. All parts should be represented by a command in your program.

  1. Take a non-zero digit (1-9). Assume the input is a valid input.
  2. Add one to this number.
  3. Multiply it with nine.
  4. Add the digits of the resulting two-digit number to each other. (Hint : int(a) gets the integer part of a.)
  5. Multiply the result with 262.
  6. Subtract 27 from this
  7. and divide the result by 3.

The program should read an arbitrary number, perform the instructions and print the result. Test your program on three different numbers. What results do you obtain?

Q2.

(1) Describe how to run a perl program in (a)window (b)Unix
(2) What is the meaning of the -w option

Q3.

What is the output of the following program?

$hero = "Batman";
print "My favorite hero is $hero.\n";
$hero = "Superman";
print 'My favorite hero is $hero.\n';

Q4.

What is 2**3?

Q5.

Suppose we run the following perl script (without -w options), what will be the output?

print "\n";

print "3:\n";
print $a + 5;
print "\n";

$b = 11;
$c = 9;
print "4:\n";
if( $b < $c) {
    print "smaller\n";
} else {
    print "not smaller\n";
}

print "5:\n";
if( $b le $c) {
    print "smaller\n";
} else {
    print "not smaller\n";
}

$d = "11ab";
$e = "22def";
$f = "aa3d";
print "6:\n";
print $d + $e + $f, "\n";

Q6

Write a program count.pl so that perl count.pl file.txt read the file file.txt and count the number of lines start with "a" or "A". The number is printed out at the end of the program.

Q7

Write a program asks for user input, if the user type in a number (either an integer or decimal number). The sum is printed out. If the user types in invalid format, the program ignores it. If the user types in "end". The program stops.

1.1
The current sum is 1.1
5
The current sum is 6.1
7.2
The current sum is 13.3
a
b
c
3.3
The current sum is 16.6
abcdf
11.2
The current sum is 27.8
0.12
The current sum is 27.92
end

Q8

What is the output of the following program?

$a = "Learning Perl";
print "1: ";
print index($a,"n");
print "\n";
print "2: ";
print index($a,"n",6);
print "\n";
print "3: ";
print index($a,"er");
print "\n";
print "4: ";
print rindex($a, "r");
print "\n";
print "5: ";
print index($a, "abc");
print "\n";
print "6: ";
print substr($a, 5);
print "\n";
print "7: ";
print substr($a, 2, 3);
print "\n";
print "8: ";
print substr($a, -3, 2);
print "\n";
substr($a, 3, 2) = "xyz";
print "9: ";
print $a;
print "\n";

Q9

What is the output of the following programs without -w option?

$a[0] = "1";
$a[1] = "3";
$a[2] = "5";
$a[3] = "1";
$a[6] = "10";
foreach(@a) {
    print $_.":";
}
print "\n";
$#a=2;
foreach(@a) {
    print $_.":";
}

Q10

A file named namelist.txt has a list of names in each line. Write a program that read in the names, arrange the names in alphabetical order and write the new list in out.txt.

Q11

What is the output of the following program?

@a = ('a'..'e');
($a[2], $a[3], $a[4]) = ($a[3], $a[4], $a[2]);
push @a, 'k';
print "@a\n";
@a2 = qw(1 2 3);
@a = (@a, @a2);
shift(@a);
pop @a;
print "@a\n";

Q12

Write a function &average take a list of numbers as arguments, and returns the average of all the elements in the arguments.
Example &average(1.1,2.2,3.3) returns 2.2.

Q13

Q14

Write a program to count the number of 'a', 'b', 'c', ... etc from a file. Stores the count in a hash. Then print out the number of 'a, 'b',... after the whole file is read. 'A','a' etc are regarded as the same.
Hint: the function lc($line) with a new string will all lowercase letters.

Q15

Construct regular expressions (match operators) for the following:

  1. any string that contains an "a" or "b" followed by any 2 characters followed by an "a" or a "b". The strings "axxb", "alfa" and "blka" match, and "ab" does not.
  2. upper case "A" followed by anything except "x", "y" or "z".
  3. any 5 digit integer.
  4. Any perl scalar variable name (including the "$"). Perl variable names can contain any alphanumeric character and the "_" character.
  5. Any word (a word is defined as a sequence of alphanumerics - no whitespace) that contains a double letter, for example "book" has a double "o" and "feed" has a double "e".
  6. Any string that contains an HTML tag and it's corresponding end tag. The following should match: <h2>Hi Dave</h2> and so should <title>The Test Answers</title>, but this should not match <title>Not a match</h2>.

Q16

Write a perl program that replaces all digits with the name of the digit, so every "0" is replaced with "zero" , "1" is replaced with "one", ... "9" is replaced with "nine".

Q17

Write a perl program that reads in an HTML file and replaces all <h1>, </h1> tag pairs with <h3>, </h3> tags.

Q18

Write a perl program that removes all HTML tags (anything that looks like an HTML tag - you don't need to check each tag name).

Q19

Write a program to retrieve all phone numbers from a file. Assume all the phone numbers are in this format: 310-222-3333. Assume that there is at most one phone number in each line. Print out the phone numbers that you obtain.

Example if the file is

This is my phone number: 510-111-2222.
This is my office phone number: 310-333-4444
801-222-7591 is Amy's number.
I don't have any cell phone.
Amy has a cell phone but I don't know her number.
Ben's cell phone number is 323-345-0000.

Then the output of the program is

510-111-2222
310-333-4444
801-222-7591
323-345-0000

Q20

Write CGI program and webpage. The webpage looks like

+ =

After you enter the number, e.g. if you input 2,3 , the output is

The sum is 5.

html file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Calculator</title>
</head>

<body>

<!-- fill your codes below -->






</body> </html>

CGI

#!/usr/local/bin/perl -wT
use CGI qw(:standard);

@names = param();
foreach $name (@names) {
   @item = param($name);
   if ($#item > 0) {
       $formdata{$name} = join(', ', @item);
   }
   else {
      $formdata{$name} = $item[0];
   }
}

#fill you codes below





sub header() { print "Content-type: text/html\n\n"; print qq(<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n); print qq(<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">$ print "<head>\n<title>\n"; print "$_[0]\n</title>\n</head>\n<body>\n"; }