Same as last time, plus
Learning Perl, ed. 6:
Chapter 2; p. 41-42
Notes (from pws); defined, int, chop
and last chapter about perl structure/syntax p. 48-51 in the notes
defined, which tells whether a variable has a valid (defined)
value or not.
int, which returns the integer part of a number (rounds down).
chop, which removes the last char of a line and returns it.
Loop control with next, last and redo and
how NOT to use them.
The =pod/=cut trick for ignoring large parts of text/code.
This course is very focused on teaching how to make nice, explicitly written
Perl code, which is easy to read by others. Therefore some of Perl's ideoms
are avoided, because they often lead to trashy, hard to understand code.
To be very concrete, use of the default variable, $_, and the diamond
operator, <>, is strongly discouraged, even if it is used in the text books.
Necessary files to complete this exercise
To download the files to your system, just press the Shift key while
you left click on the blue link. Follow the instructions.
We have reused some of the files from earlier exercises.
You can play around with these files as much as you like. If you change or
destroy them, just download them again.
Remember to write #!/usr/bin/perl on the first line of
All the following exercises have to be done in Perl, except for 2 and parts of 4.
You might want to consider what would happen if you feed an empty file to your program.
- Make a program that asks for two numbers (both integers), calculates the
integer in the middle (average or mean if you will) of the two numbers and displays it . It can be done
in at least 4 different ways. If there are two integers in the "middle",
it does not matter, which one you choose.
- Create a file with two integers (one on each line) and feed that
file using redirection to your above "mean" program.
- Construct a program that counts the number of lines in the input, and
displays the result. Try it using keyboard input. Use ctrl d to
end the input. Count the number of lines in ex1.dat. Redirect input
as earlier learned (cat ex1.dat | your_program). There are 1675 lines.
- Construct three files from ex1.dat (using already learned UNIX
techniques). Each file should contain one column of numbers from
ex1.dat. Now make a program that sums numbers from input and displays
the sum of the numbers.
Feed the columns from the three files, one at a time, to the program using
redirection. Be careful you don't delete your program by a stupid redirection
error (I did). The sums are approx. Col 1; -904.4143, Col 2; 482.8410,
Col 3; 292.05150.
- Based on the previous program, now make a new one that calculates
the mean value of the columns. Also create a file with the numbers 9-1
and feed that to the program. What is the result from your program
and what should it be, based on calculations in your head?
- Now make a program (perhaps based on the previous one) that counts
the number of positive and negative numbers in a column. Also count
the zeroes in the column, if there are any. Display the result.
- Now make a program (perhaps based on the previous one) that finds the
maximum number in a column. Display the result.
- Now make a program that finds the minimum number in a column and display the result.
- It begins to get trivial. Now make a program that does all of the above,
that is; calculate the sum, the number of lines, the mean value, the number
of positive, negative and zero numbers, the maximum and the minimum value.
Display the all results clearly so there is no doubt about
what you display.
- Write a program that converts temperatures from Fahrenheit to Celsius or
visa versa given input like "36F" of "15C" ( F = (C * 9/5) + 32 ). Hint: Study chop in the notes.
- Write a script that prints a "bulls-eye" to STDOUT using chracters like,
'.', '+', '*', '#', etc. Here is an example.
I only used: for, if, elsif, print, +, -, /, **, int, sqrt for this 40x40 example.