Skipping over "if" subroutine...

Posted on 2011-04-27
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
    LVL 31

    Expert Comment

    In line 23
    $ans = <>;

    $ans = <STDIN>;
    LVL 3

    Expert Comment

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

    Accepted Solution

    $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 )

    Author Comment

    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

    Yes, after line 23, use

    chomp $ans;

    Before the if condition.

    Author Closing Comment

    THANK YOU!!!
    LVL 26

    Expert Comment

    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

    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!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Android App Development

    This course will involve creating widgets, customize list view, grid view, spinners, etc. Creating applications using audio, video, and SQLite database. Ending with publication on Google Play.

    Suggested Solutions

    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    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…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now