Perl programming exercises 2

From "A B C"
Jump to navigation Jump to search

Perl programming exercises 2


Small programming exercises for an introduction to Perl.


 

Basic Perl Programming - Programming exercises

Boris Steipe acknowledges contributions by Jennifer Tsai, Sanja Rogic and Sohrab Shah.

Here are programming exercises that focus on translating a concept into a working script.

The preceding section covers a section I have called syntax examples ... they are simple tasks that ask you to write functioning code syntactically correct.


For each task you will find

  1. a description of the task your code should achieve;
  2. some hints how to go about solving it - which functions you might use or which strategy; and
  3. sample code for reference if you are stuck. Should you really need to look up the samples, carefully study the code, put it away and then write your own script from scratch, with different code and perhaps some variation in function. If you merely copy code, or read it with mild interest and move on, you will probably be wasting your time.
Don't be satisfied until you understand what you are doing.


Tasks

Hello World

 

Executable program

Write a Perl program helloWorld.pl that prints out "Hello World" (or whatever you fancy) to the terminal. Make your program executable (chmod u+x helloWorld.pl) so that you don't need to invoke the Perl interpreter explicitly from the command line (i.e. just "$ ./helloWorld.pl" should run it, you shouldn't need to type "$ perl helloWorld.pl").

 

Hints:


 

cat()

 

Keyboard input

Write a Perl program cat.pl that prints to the terminal a single line that you type at the keyboard.

 

Hints:


 

lc()

 

More keyboard input

Write a Perl program lc.pl that reads one or many lines from STDIN, converts them to lowercase and prints them to the terminal. Use this interactively, typing input (end by typing <ctrl>D), then use this by redirecting a textfile to your program, then "pipe" the output of the Unix "ls" command into your program.

 

Hints:


 

max

 

Condition

Write a Perl program max.pl that prompts for and reads two numbers from STDIN, and outputs the larger of the two numbers to the terminal. Remember to consider the case that the numbers may be equal.

 

Hints:


 

max (with subroutine)

 

Subroutine

Rewrite max.pl so the comparison is done in a subroutine: pass the two numbers as arguments into a subroutine and return the larger of the two. Such a program may be a useful framework for comparing two datasets with a non-trivial metric. Instead of simply picking the larger value, the subroutine could compare according to some sophisticated algortithm

 

Hints:


 

anagram

 

Array

Write a Perl program anagram.pl that reads a string from STDIN and returns ten random permutations of this string. This will require a number of concepts and techniques of working with arrays - defining an array, assigning values to an array, or to individual fields of an array, using a variable as an index to an array in order to read from or write to specific fields, and more. First split your string into individual elements of an array. Use a subroutine that randomizes this array by looping over every position of the array, and swapping the contents of this position with a randomly chosen other position of the array, except itself. Write this in pseudocode first. The Perl functions you will need are split(); and rand();.

 

Hints:


 

anastring

 

Substring calisthenics

Copy anagram.pl to a file named anastring.pl and use the Perl strlen(); and substr(); functions to permute the string (in place !) instead of shuffling fields of an array. Make a point of programming this incrementally step by step, writing output as you go along to make sure you are doing it right. Of course you could also shuffle using the split() and join() functions on a string ... but that would not be "in place".

 

Hints:


 

anastring (with commandline input)

 

Commandline arguments

Modify anastring.pl so you can pass the number of permutations to the program in the commandline ( via @ARGV ), make sure that the default is 1, if no argument is given. This tool could be part of a routine to generate random data to test statistical significance.

 

Hints:


 

sort

 

Sorting

Write a Perl program sort.pl that takes in strings (e.g. names) from STDIN, stores them in an array, sorts them in alphabetical order, using the Perl sort(); function and prints them out to the terminal.

 

Hints:


 

fastaParser

 

Hash (associative array)

Write a Perl program fastaParser.pl that takes in a FASTA file of any protein as input and outputs it as three-letter amino acid code separated by spaces to the terminal. Use a hash to store the mapping between the one-letter amino acid code and the three-letter amino acid code.

 

Hints:


 

factorial

 

Iteration and recursion

Write a Perl program factorial.pl that takes in a number as input and calculates the factorial of that number. Note that this can be done in (at least) two ways: the first way is to use a for loop in the body of the program...

 

Hints:


 

factorial(recursive)

  (OPTIONAL) ...the second way is to use a subroutine recursively to yield the factorial of a number. Try programming it this way as well.

 

Hints:


 

 

Further reading and resources