Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Skipping over "if" subroutine...

Posted on 2011-04-27
Medium Priority
Last Modified: 2012-08-13
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

Question by:Member_2_4213139
  • 2
  • 2
  • 2
  • +2
LVL 31

Expert Comment

ID: 35475453
In line 23
$ans = <>;

$ans = <STDIN>;

Expert Comment

ID: 35475465
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: http://www.comp.leeds.ac.uk/Perl/matching.html

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.
LVL 10

Accepted Solution

jeromee earned 2000 total points
ID: 35475516
$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 )
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 35475532
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?
LVL 31

Expert Comment

ID: 35475543
Yes, after line 23, use

chomp $ans;

Before the if condition.

Author Closing Comment

ID: 35475547
LVL 27

Expert Comment

ID: 35475594
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.
LVL 10

Expert Comment

ID: 35475641
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!

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question