Skipping over "if" subroutine...

Regardless of whether the answer is "Y" or "y" the script skips over the "if Y" subroutine... HELP!

sub valueSort {
        $carMake{$a} cmp $carMake{$b};

%carMake = (
                "FORD" => "Ford Motor Company",
                "GM" => "General Motors",
                "CHRYS" => "Chrysler Corporation",
                "OTHER" => "a company (Not Listed)",

print "WELCOME to the Auto-something Information Requestor\n";
$now = localtime();
print "Today\'s date is ", $now, "\n";
print "Please enter the following information:\n";
print "Your name: ";
$name = <STDIN>;
# $number = read(STDIN, $firstName);
print "Would you like more information?  (Y or N): ";
$ans = <>;
if ($ans eq 'Y' or $ans eq 'y')
print "What is your Phone Number (xxx-xxx-xxxx): ";
$phone = <>;
print "\n";
print "Your address:\n";
print "\tStreet: ";
$stAddress = <>;
print "\tCity, State, Zip: ";
$cSZ = <>;
print "Information will be sent to your address at\n";
printf "\t% s", $stAddress;
printf "\t% s\n", $cSZ;
print "\n";
print '"Car makes" information available:', "\n";
print "_________________________\n";     
foreach $key (sort valueSort(keys(%carMake))) {
   printf "% -2s\|%s\n", $key, $carMake{$key};
   print "_________________________\n";     
print "\n";
print "What is the \Uname\E of the car brand you are interested in? ";
$carType = <>;
chomp $carType;
$carType = uc($carType);
print 'The car is manufactured by ', qq/"$carMake{$carType}."/;
print "\n\n";
print "Thank you, ", $name, "for your interest.\n\n";

Open in new window

Who is Participating?
$ans needs to be chop'd (to remove the \n) or loosen the test
I would replace this:
   $ans = <>;
   if ($ans eq 'Y' or $ans eq 'y')

with this:

   my $ans = <STDIN>;
   if ($ans =~ /^y/i )
In line 23
$ans = <>;

$ans = <STDIN>;
Is appears to me that $ans is a string and 'y' and 'Y' to which you are comparing it are single characters, hence they are different types and it never meets the condition to enter the if statement... it's not that the character 'y' or 'Y' does not start the string $ans, just they they're different types.

See this site for some help on string comparisons in perl:

I suggest you change it to read:

if ($ans eq "Y" or $ans eq "y")

And also consider that $ans might be more than 1 character.  Therefore ans will not match the single characters yet again.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Member_2_4213139Author Commented:
Your change did nothing... (STDIN is referenced above it) ...

OK, so I changed it to "YES" or "yes" and it STILL bypassed the routine.  What now?
Yes, after line 23, use

chomp $ans;

Before the if condition.
Member_2_4213139Author Commented:
xelous answer is incorrect.  In perl, there is absolutely no logical difference between any of the following (except what string will match):

$ans eq 'y'
$ans eq "y"
$and eq "yes"

Performance-wise, it is (very) slightly better to use 'y' as perl has to look at "y" to see if there is anything it needs to interpolate (eg 'y$n' is literally y$n but "y$n" becomes something else like yno).

jeromee and farzanj are correct.  You need to chomp any input string that will have a newline.  Alternately, you can use a regex to match as jeromee suggested.
I'm glad I was able to help, ryanasalazar.
I would also suggest that you use "use strict;" and start declaring your variables. It's good practice and will help you catch errors early on.

Happy Perl'ing!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.