Implement the following instructions in a Perl program. All parts should be represented by a command in your program.
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?
(1) Describe how to run a perl program in (a)window (b)Unix
(2) What is the meaning of the -w option
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';
What is 2**3?
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";
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.
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
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";
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 $_.":";
}
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.
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";
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.
strict?my in main part?my in subroutine
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.
Construct regular expressions (match operators) for the following:
<h2>Hi Dave</h2> and so should <title>The Test Answers</title>,
but this should not match <title>Not a match</h2>. 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".
Write a perl program that reads in an HTML file and replaces all <h1>, </h1> tag pairs with <h3>, </h3> tags.
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).
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
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.
<?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>
#!/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";
}