Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie Perl question

Status
Not open for further replies.

rhysmeister

Programmer
May 29, 2003
54
0
0
GB
Something is up with my if statement...

Code:
$choice = <STDIN>; # read the choice from the keyboard
if($choice == "Y")
{
	stuff here;
}
elsif($choice == "N")
{
	stuff here
}

The statement always does the first if statement whatever is input. Probably an easy one!
 
Rhys,

== is used to compare numbers. Try using "eq" or "ne" (equal or not equal, respectively):

my $string = 'hello';

if($string eq "hello") {
print "equal";
} else {
print "not equal";
}
 
Also, don't forget to chomp any end-of-line characters from the input string.
Code:
chomp($choice = <STDIN>);
Cheers, Neil
 
Code:
chomp ($choice = <STDIN>); # read the choice from the keyboard

if ($choice eq "Y") {
  print "YES\n";
} else {
  print "NO\n";
}


Kind Regards
Duncan
 
Wow quick! Thanks. Done that but it just skips to my else section of the staement.

Code:
print("Configure General options? Y/N");
$choice = <STDIN>; # read the choice from the keyboard
if($choice eq 'Y')
{
	
}
elsif($choice eq 'N')
{	
	
}
else # have to finish with an else!!!
{
	print "An error has occured!";
}
 
are you using a lowercase 'y' rather than 'Y'?


Kind Regards
Duncan
 
Ah right, I remember, use chomp to get rid of the newline character!! Thanks to both of you!
 
Code:
chomp ($choice = <STDIN>); # read the choice from the keyboard

if (lc($choice) eq "y") {
  print "YES\n";
} else {
  print "NO\n";
}


Kind Regards
Duncan
 
Are you allowing lowercase input? If 'n' or 'y' is entered, your else statement WILL always be reached. Try this instead:
Code:
print("Configure General options? Y/N");
$choice = <STDIN>; # read the choice from the keyboard
if($choice =~ /^Y$/i)
{
    
}
elsif($choice =~ /^N$/i)
{    
    
}
else # have to finish with an else!!!
{
    print "An error has occured!";
}

--G
 
Or use Duncan's solution, which is much more elegant than mine. *sigh*

--G
 
Thanks all of you, got to be the fastest forums I've ever been on! Is there an uppcase() function like C has?
 
uc - uppercase
lc - lowercase
lcfirst - lowercase first letter
ucfirst - uppercase first letter
 
by the way...

you need to chomp the input to remove the line ending

eq is used for textual comparisons and == for numeric

it probably makes sense to reduce if statements by just using the else instead of another elsif


Kind Regards
Duncan
 
You can also use metacharacters in strings to accomplish the same thing:
Code:
$mystring = "fee \Ufi fo fum";
print $mystring';
output:
fee FI FO FUM

or
Code:
$mystring = "\ufee fi fo fum";
print $mystring';
output:
Fee fi fo fum

\U - converts everything after it in a string to uppercase
\L - converts everything after it in a string to lowercase
\u - converts next character only to uppercase
\l - converts next character only to lowercase

These are handier if you want to make multiple changes in the same string:
Code:
# capitalize each word
$mystring = "\ufee \ufi \ufo \ufum";

If you want to convert entire strings one way or the other, though, I find lc and uc to be syntactically neater.

Even if Duncan did remember them and I didn't.

But I'm not bitter. ;-)

--G
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top