CSCI 344
Perl File I/O
NOTE: Starting with this lab the
turn in instruction are changing. You must copy all your programs
to the turn in directory even if I look at your program in lab.
For each problem I will give the filename you should use.
I am also changing the names of the turn-in directories to match the
name of the lab. For example, the name of the turn in directory
for this lab is: /user/projects/csci344/perl_file_io.
Write the following Perl scripts. You may use the Internet as a
reference for Perl and as a reference for UNIX/Linux
commands, but you may not search for
solutions to these problems--that would completely defeat the purpose
of doing them.
Here is a link to a good intro to Perl: perldoc.perl.org/perlintro.html
You may NOT use any UNIX/Linux command or utility to solve these problems. They can all be solved with basic Perl.
1) (weight 2) Write a Perl subroutine
for each of the following descriptions. You will have to write
code to test your subroutines and to demonstrate to me that they work.
Put your code in the file: args.
a) Returns the number of arguments to the subroutine.
b) Returns a list of all the arguments sorted alphabetically.
c) Returns a list of all the arguments with duplicates removed.
2) (weight 3) Write a program that echoes input lines with line numbers:
Input:
first line
second line
third line
...
last line
Output:
1 first line
2 second line
3 third line
999 last line
Right justify the line numbers and pad them according to the size of
the input. That is if there are more than 9 lines, pad all the
numbers so they print in 2-columns, if there are more than 99 lines,
pad the numbers so they print in 3-columns, and so forth.
If arguments are given on the command line, assume they are all
filenames and read all of them sequentially. If no arguments are
given, read from standard input.
Hint: Perl makes this behavior very, very easy to implement, but
don't blurt out the answer so other students have to remember or find
it. Put your program in the file myecho.
3) (weight 3) Write a script that reads one
or more files containing comma-separated-values (.csv) and print them
in tabular form. For example:
Input:
a,b,c,d,e
1,2,3,4,5
v,w,x,y,z
Output:
a b c d e
1 2 3 4 5
v w x y z
As a default, make all the columns 5 characters wide. If the
first command line argument starts with a -, assume it is an integer
that specifies the column with. Except for the possibility of a
column width as the first command line
argument, all the other command line arguments should be files:
$ tprint data1.csv data2.csv
$ tprint -10 data1.csv data2.csv
If no command line arguments are specified, do nothing.
The function split, which splits a string into elements, is very helpful
@elements = split /,/, $line;
Using a pattern (Perl's name for regular expression matches) makes it easier to check if the first command line
argument is a - followed by a digit. The following returns true if the variable matches the regular expression:
$var =~ /regular expression goes here/
Put your program in a file
called tabprint.