We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Skipping over "if" subroutine...

Member_2_4213139
on
Medium Priority
290 Views
Last Modified: 2012-08-13
Regardless of whether the answer is "Y" or "y" the script skips over the "if Y" subroutine... HELP!
#!/usr/bin/perl

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);
Sleep(1);
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

Comment
Watch Question

CERTIFIED EXPERT

Commented:
In line 23
Change
$ans = <>;

To
$ans = <STDIN>;

Commented:
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.
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

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

xelous,
OK, so I changed it to "YES" or "yes" and it STILL bypassed the routine.  What now?
CERTIFIED EXPERT

Commented:
Yes, after line 23, use

chomp $ans;

Before the if condition.

Author

Commented:
THANK YOU!!!
CERTIFIED EXPERT

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.

Commented:
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!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.