Thursday, May 1, 2008

Perl: Conditionals

Perl 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){
print "The string is empty\n";
}elsif (length($a) == 1){
print "The string has one character\n";
}elsif (length($a) == 2){
print "The string has two characters\n";
}else {
print "The string has lots of characters\n";
}

No comments:

Post a Comment