June 28th, 2013, 10:45 AM
-
Basic Perl Program Help
Can someone help me determine how to convert the text entered in <STDIN> to lowercase so that however the user enters 'heads' or 'tails', the block executes? Here is what I have so far:
#!/usr/bin/perl -w
print "Please type in either heads or tails: ";
# The <STDIN> is the way to read keyboard input
$answer = <STDIN>;
chomp $answer;
while ( $answer ne "heads" and $answer ne "tails" ) {
print "I asked you to type heads or tails. Please do so: ";
$answer = <STDIN>;
chomp $answer;
}
print "Thanks. You chose $answer.\n";
print "Hit enter key to continue: ";
# This line is here to pause the script until you hit the
# carriage return
# The input is never used for anything.
$_ = <STDIN>;
if ( $answer eq "heads" ) {
print "HEADS! you WON!\n";
} else {
print "TAILS?! you lost. Try again!\n";
}
June 28th, 2013, 12:18 PM
-
The lc function returns a lower case version of the expression passed to it.
Perl Code:
my $lc_string = lc "FOOBAR"; # $lc_string now contains "foobar"
June 28th, 2013, 02:13 PM
-
Thank you!!!