CS 655 Info. Delivery on the Internet - Homework 2 - Name:_______________ Assigned: Monday, September 29 Due: Friday, October 13 (Submitted by the Handin System) o Submit your codes electronically by the handin system with the submission ID hwk2. (50 points) 1. Write a Perl program to read the starting term, a, the common ratio, r, and the number of term, n of a geometric series from the command line and calculate the sum of this series. For example, your program called "geometry.pl", a possible session would look like: (10 points) > geometry.pl 2 3 6 The sum of the geometric series is 728. Hint: Use @ARGV or shift to retrieve the command-line arguments. Ans: #!/usr/bin/perl my ($a, $r, $n) = @ARGV; $s = $a * ($r ** $n - 1) /($r - 1); print "The sum of the geometric series is $s.\n"; 2. Write a Perl program to read a pattern and a file name from the command line and search a that file for the pattern. If the pattern is found, print those lines containing the pattern. A possible session would look like: (10 points) > grep.pl ate data John ate an apple. Mary had a plate of salad. where 'ate' is the pattern and 'data' is the file name. Ans: #!/usr/bin/perl my ($pattern, $filename) = @ARGV; open (INFILE, "$filename"); while() { if (/$pattern/) { print; } } close(INFILE); 3. Write a program that takes a text file as input and displays a count of lines and words in a file. A possible session would look like: (15 points) > wc.pl data The file contains 9 lines and 39 words. where 'data' is the file name. Hint: See word_table.pl on Page 363 Ans: #!/usr/bin/perl $filename = shift; open(INFILE, "<$filename"); my ($lines, $words) = (0, 0); while() { $lines++; @line_words = split /[ \.,;:!\?]\s*/; $words += scalar(@line_words); } print "The file contains $lines lines and $words words.\n"; 4. Write a Perl program with the following specifications. (15 points) Input: A (data) file that has the following format. Programming the World Wide Web+Robert W. Sebesta+Addison Wesley+2003 Computer Networks, 4th Ed.+Andrew S. Tanenbaum+Prentice Hall+2002 : more book entries : Output: A HTML document that consists of a table with the title row Book Title, Authors, Publisher, and Year following by data rows which indicate the book title, authors, publisher, and year of each entry. A running session could be like: > booklist.pl < data > booklist.html or > booklist.pl data booklist.html The booklist.html would like like: Book list
Book Title Authors Publisher Year
Programming the World Wide Web Robert W. Sebesta Addison Wesley 2003
Computer Networks, 4th Ed. Andrew S. Tanenbaum Prentice Hall 2002
where "booklist.pl" is your Perl program, "data" is the input file, and "booklist.html" is the generated HTML document. Hint: Use CGI.pm and the table function (See split.pl, html_table.pl, and conelec1.p). "print header()" is not required to create a HTML. You can verify the correctness of your generated HTML file by any Web browser. Note: > and < indicates the output and input stream in UNIX respectively. Ans: #!/usr/bin/perl -w use CGI ":standard"; print start_html("Book list"), "\n"; push @books, th(["Book Title", "Authors", "Publisher", "Year"]); while (<>) { @book = split /\+/; push @books, td(\@book); } print table({-border => "border"}, Tr(\@books) ); print end_html();