Online Perl Course/Quiz04

From Wsms

Jump to: navigation, search

up

Contents

Quiz 4

See: http://wsms.wikiplanet.com/html/opc/quiz04.html

Question 1

(20 points) Use the test program to make and test a pattern that matches any string containing fred. Does it match if your string is Fred, frederick or Alfred?

Testprogram:

#!/usr/bin/perl -w
#Program to test regular expressions
#This program reads in a regular expression and
#then allows the user to enter strings to test
#written by J. McGerald 2001-07-11

print "Please enter regular expression - \n";
chomp($regex = <STDIN>);
print "OK, what strings do you want to evaluate ?\n";
while (<STDIN>) {
  chomp;
  if (/$regex/) {
    print "$_ is True!\n";
  } else {
    print "$_ is False\n";
  }
  print "please enter another string or <CTRL>Z to quit: \n";
}

Output:

[ggeller@arthur quiz4]$ ./1.pl
Please enter regular expression -
fred
OK, what strings do you want to evaluate ?
fred
fred is True!
please enter another string or <CTRL>Z to quit:
alfred
alfred is True!
please enter another string or <CTRL>Z to quit:
Fred
Fred is False
please enter another string or <CTRL>Z to quit:

Fred is not matched. frederick is matched. Alfred is matched.

Question 2

(20 points) Write a new program that prints out any input line that mentions Wilma. Any other lines can be skipped. Modify it to also print out lines containing wilma.
Note: this is not the test program! You need to write it.
Answer A:

#!/usr/bin/env perl
use warnings;
use strict;
$|++;

while (<>) {
  chomp;
  if (/Wilma/) {
    print "$_\n";
  }
}

Answer B:

#!/usr/bin/env perl
use warnings;
use strict;
$|++;

while (<>) {
  chomp;
  if (/[Ww]ilma/) {
    print "$_\n";
  }
}

Output:

[ggeller@arthur quiz4]$ ./2.pl < scratch
wilma
Wilma

Question 3

(20 points)
Write a program that prints out any line that mentions both fred and wilma.
Answer:

#!/usr/bin/env perl
use warnings;
use strict;
while (<>) {
  chomp;
  if (/wilma/ and /fred/) {
    print "$_ \n";
  }
}

Output:

[ggeller@arthur quiz4]$ ./3.pl < scratch
fredwilma
wilmafred
fred and wilma
wilma and fred

Question 4

(20 points)
Write a program that prints out any line ending with white space (other than a new line). Put a marker character (such as “$$”) at the end of the line to make the white space visible.
Answer:

#!/usr/bin/env perl
use warnings;
use strict;
while (<>) {
  chomp;
  if (/\s$/) {
    print "$_\$\$\n";
  }
}

Output:

[ggeller@arthur quiz4]$ ./4.pl < scratch
 $$
        $$
== $$

Question 5

(20 points)
Write a program that will process the programs that you have written so far for this course and will add a comment line with your name immediately after the shebang line. Keep a backup of the original file. Test it on one or two of your program files. Copy them to a new folder first so that if the program fails you will not destroy your original code! Answer:

#!/usr/bin/env perl
use warnings;
use strict;
$|++;

my $author='#Author: George Geller';

$^I= ".bak";
while (<>) {
  s[(^\#\!.*perl.*\n$)][$1$author\n\n]; # any line starting with #! and containing perl
  print;
}

Usage:

[ggeller@arthur quiz4]$ ./5.pl *.pl
Personal tools