User Tools

Site Tools


cs498gpl:perl_exercises

Perl Exercises

  • Deadline: Fri, Feb 21, 11:59pm
    • Use the Perl exercises dropbox on ecampus. Don't dropbox any input or output files.
    • ecampus will reject files with the .pl extension, so either rename your files to .txt or zip your .pl files and dropbox them.
  • NOTE: Unless otherwise noted, all work listed here is mandatory and counts toward your assignments grade.
  • NOTE 2: (Exercises 2-6 can be completed using regular expression search and replace or standard functions such as length or split.)
  • NOTE 3: You must insert your name, program description, course name, and semester in a comment block at the top of each program or else you will lose points.

Exercise 1 - word length occurrences

Download english.sorted to your perl exercises folder. The english.sorted file contains a list of words of variable length. Write perlex1.pl to find the number of occurrences of each word length in english.sorted. That is, find and print the number of words in english.sorted that are one character long, two characters long, three characters long and so on.

Sample output:

Word length	Occurrences

1		27
2		104
3		1038
4		3501
5		7179
6		11758
7		15892
...
...

Exercise 2 - short words

Write perlex2.pl, and run it to extract all the words from english.sorted that are 7 characters or less and save them to a new word list, short.words. Test the script first with

perl -w perlex2.pl < english.sorted

to print the short words. Then, if you are satisfied that it works,

perl -w perlex2.pl < english.sorted > short.words

to save the short words to short.words.

(After we cover regular expressions, do this exercise again using regular expression matching. Also dropbox perlex2_re.pl.)


Exercise 3 - sort roster

Copy or download a class roster file to your perl exercises folder. Write perlex3.pl to read each line of the roster into an array, swapping the order of the last name and first name in the process. Then sort the roster by the first letter of the first name, and print the array.


Exercise 4 - roster to hash

Write perlex4.pl to read each line of a different class roster file into a hash. The hash key is the student's id (not email), and hash value will be the student “lastname, firstname” (string). Then, in a separate foreach loop, print each student's id (key), followed by their name (value). The Perl substr (substring) function could be helpful.


Exercise 5 - roster w/ command line arguments

Write perlex5.pl (using code from previous exercises) to accept and run according to the following command line arguments:

  • no arguments: display command usage along with an explanation of all other command line arguments
  • -help, --help or -h: display command usage along with an explanation of all other command line arguments
  • -first or --first: display the class roster sorted by first name
  • -last or --last: display the class roster sorted by last name
  • -id or --id: display the class roster sorted by student ID

Exercise 6 - read an entire file into a variable

See http://www.perlhowto.com/read_the_contents_of_a_file_into_a_variable. Reading an entire file into a variable in Perl involves manipulating the $/ special variable which is mentioned at http://www.kichwa.com/quik_ref/spec_variables.html .

Write perlex6.pl to read the class roster (roster.txt) into a variable and print the variable. Next, attempt to read all the student IDs in the roster file into an @ids array.


Exercise 7 - extract data from html

Save the html source of https://www.monmouth.edu/department-of-csse/news-events/ to your perl/exercises directory. On Linux or macOS, you can also use the wget or curl command to retrieve and save the html source file:

 $ wget https://www.monmouth.edu/department-of-csse/news-events -O csse_news.html
 
 or
 
 $ curl https://www.monmouth.edu/department-of-csse/news-events > csse_news.html

Use a Perl script perlex7.pl to extract all the headlines such as “CS Alumnus Featured in School of Science Newsletter” into an array and print the array. Also, extract the corresponding anchor names such as “barnathan1” in “<a class=“archorMargin” name=“barnathan1”>” into another array and print that array.

Retry this exercise using a Web automation module such as LWP::Simple or WWW::Mechanize to retrieve the HTML. Also dropbox perlex7_lwp.pl.



cs498gpl/perl_exercises.txt · Last modified: 2025/02/24 05:22 by jchung

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki