User Tools

Site Tools


cs498gpl:perl_exercises

Perl Exercises

  • Deadline: Tue, Feb 24
    • 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.txt to your perl exercises folder. The english.sorted.txt 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.txt. That is, find and print the number of words in english.sorted.txt 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.txt that are 7 characters or less and save them to a new word list, short.words.txt. Test the script first with

perl -w perlex2.pl < english.sorted.txt

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

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

to save the short words to short.words.txt.

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


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/news/archives 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/news/archives -O mu_news.html
 
 or
 
 $ curl https://www.monmouth.edu/news/archives > mu_news.html

Use a Perl script perlex7.pl to extract all the headlines such as “Prof. Lewandowski Co-Authors Second Edition of Leading Text on Relationship Science” into an array and print the array. Also, extract the corresponding links such as https://www.monmouth.edu/news/prof-lewandowski-co-authors-second-edition-of-leading-text-on-relationship-science/ 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: by jchung

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki