Q1 2pts each, -1 pt for every mistake a. Charles is the instructor of PIC40A. $name is the instructor of PIC40A.\n b. out1: 4 out2: -5 c. out1: def2abc out2: 5 out3: defdefdef out4: 0 d. a-XkX-a abc-XXX-abc Q2. 1pt for each input a. out1: abc12 out2: 2 out3: a out4: bc1 out5: 5bc1h out6: h1cb5 b. out1: 5 out2: 14 out3: -1 out4: 14 out5: Ja out6: PIC40A, P. PIC20A, Java. Q3 a. 2pts apple|orange b. 2pts /\d[A-Z]{3}\d{3}/ c. 2pts /(.)(\d)(\1)(\2)(\1)/ d. 4pts begin\s([a-zA-Z]+)\s.*\send\s\1($|[^a-zA-Z]) Q4 5pts %hash = ("Charles", "Prof A", "Mary", "Prof B", "Lori", "Prof A", "Amy", "Prof A", "Ben", "Prof C", "Dan", "Prof D", "Ken", "Prof B"); while( ($student, $prof) = each(%hash)) { if(exists($students{$prof})) { $students{$prof}++; } else { $students{$prof} = 1; } } print $students{"Prof A"}; Q5 10 pts open FH, "data.txt"; while(){ chomp; @array = split(/, /, $_); $sum = 0; foreach(@array) { $sum+=$_; } $length = @array; print $sum/$length, "\n"; } Q6 5 pts each (a) [\w\-\.]+\@[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4} (b) open IN, "email.html"; open OUT, ">>list.txt"; while() { chomp; if(/[\w\-\.]+\@[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4}/) { print OUT $&,"\n"; } } (c) while(<>) { chomp; s/([\w\-\.]+)\@ucla\.edu/$1 AT ucla DOT edu/; print $_, "\n"; } or open IN, "email.html"; open OUT, ">>list.txt"; while() { chomp; if((/[\w\-\.]+\@[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4})/) { print OUT $1,"\n"; } } Q7. #!/usr/local/bin/perl -wT use CGI qw(:standard); # not the complete list of states %state_sale_tax = (Alabama => 4, Alaska => 0, Arizona => 5.6, Arkansas => 5.125, California => 7.25, Colorado => 2.9, Connecticut => 6, Delaware => 0, Florida => 6, Georgia => 4, Hawaii => 4, Idaho => 5, Illinois => 6.25, Indiana => 6, Iowa => 5, Kansas => 5.3, Kentucky => 6, Louisiana => 4); if(param()) { @names = param(); foreach $name (@names) { @item = param($name); if ($#item > 0) { $formdata{$name} = join(', ', @item); } else { $formdata{$name} = $item[0]; } } # your codes $price = $formdata{"price"}; $state = $formdata{"state"}; $tax = $state_sale_tax{$state}; header("Price"); print "

The price is ", &price_after_sale_tax($price, $tax), "

"; footer(); } else { # your codes header("Price"); print qq(); print qq(); print qq(
); print qq(price:

); print qq(state: "; print qq(

\n
); footer(); } # answer of part (a) sub price_after_sale_tax { $len = @_; if($len == 0) { return 0; } elsif($len == 1) { return $_[0]; } else { return $_[0]*(1+$_[1]/100); } } # other codes sub header() { print "Content-type: text/html\n\n"; print qq( \n); print qq(); print "\n\n"; print "$_[0]\n\n\n\n"; } sub footer() { print "\n\n"; } #!/usr/local/bin/perl -wT # survey_simple.cgi # This CGI script will generate and process a from requesting information # from the user. use CGI qw(:standard); $answer = 31; if(param()) { @names = param(); foreach $name (@names) { @item = param($name); if ($#item > 0) { $formdata{$name} = join(', ', @item); } else { $formdata{$name} = $item[0]; } } $guess = $formdata{"guess"}; if($guess =~ /\D/) { header("guess"); print "

Good guess, but nope. Try a number next time.

"; content(); footer(); } else { if($guess > $answer) { header("guess"); print "

Good guess, but nope. Try lower.

"; content(); footer(); } elsif($guess < $answer) { header("guess"); print "

Good guess, but nope. Try higher.

"; content(); footer(); } else { header("guess"); print qq(

Congratulations! You got it.

); print qq(

Care to try again.

); footer(); } } } else { header("Guess a number"); content(); footer(); } sub content() { print <<"HTML code";

I'm thinking of a number between 1 and 100.

What's your guess?

HTML code } sub header() { print "Content-type: text/html\n\n"; print qq( \n); print qq(); print "\n\n"; print "$_[0]\n\n\n\n"; } sub footer() { print "\n\n"; } 9. a) odd number in hash b) my $abc = .. c) &hello d) /\/pattern\// e) $x