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

x
?
Solved

Skipping over "if" subroutine...

Posted on 2011-04-27
8
Medium Priority
?
281 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

0
Comment
Question by:Member_2_4213139
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 31

Expert Comment

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

To
$ans = <STDIN>;
0
 
LVL 3

Expert Comment

by:xelous
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.
0
 
LVL 10

Accepted Solution

by:
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 )
0
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

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

Expert Comment

by:farzanj
ID: 35475543
Yes, after line 23, use

chomp $ans;

Before the if condition.
0
 

Author Closing Comment

by:Member_2_4213139
ID: 35475547
THANK YOU!!!
0
 
LVL 27

Expert Comment

by:wilcoxon
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.
0
 
LVL 10

Expert Comment

by:jeromee
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!
0

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