Of course Perl also allows if/then/else statements. These are of the
following form:
if ($a)
{
print "The string is not empty\n";
}
else
{
print "The string is empty\n";
}
For this, remember that an empty string is considered to be false. It
will also give an "empty" result if $a is the string 0.
It is also possible to include more alternatives in a conditional
statement:
if (!$a) # The ! is the not operator
{
print "The string is empty\n";
}
elsif (length($a) == 1) # If above fails, try this
{
print "The string has one character\n";
}
elsif (length($a) == 2) # If that fails, try this
{
print "The string has two characters\n";
}
else # Now, everything has failed
{
print "The string has lots of characters\n";
}
In this, it is important to notice that the elsif statement really
does have an "e" missing.
Find a fairly large file that contains some text and some blank lines.
The file ~nik/WWW/Misc/electricity.txt is pretty good because it's
funny apart from anything else. It was originally posted to our local
news system by David O'Brien.
From the previous exercise you should have a program which prints out
the password file with line numbers. Change it so that works with the
text file. Now alter the program so that line numbers aren't printed
or counted with blank lines, but every line is still printed,
including the blank ones. Remember that when a line of the file is
read in it will still include its newline character at the end.
_________________________________________________________________