Question:
Help a beginner with perl if/elsif/else stuff?
littelbro14
2008-12-02 19:44:07 UTC
I'm a total beginner with Perl, so forgive me if this is a pretty simple fix. Anyway, every time I run through this, it only ever takes the first path. What's up?

print "Which gas type will you be using?\n";
print "(Regular, Plus, Premium, Diesel)\n";
$type = ;
chomp($type);
if ($type = "regular"){
$type = $regular;
print "You use regular gas and will pay about \$";
print $type;
print " per gallon.\n";
} elsif ($type = "plus"){
$type = $plus;
print "You use Plus gas and will pay about \$";
print $type;
print " per gallon.\n";
} elsif ($type = "premium"){
$type = $premium;
print "You use Premium gas and will pay about \$";
print $type;
print " per gallon.\n";
} elsif ($type = "diesel"){
$type = $diesel;
print "You use Diesel gas and will pay about \$";
print $type;
print " per gallon.\n";
} else {
print "$type is not a recognized gas type!";
}
Four answers:
anonymous
2008-12-02 22:52:51 UTC
As others have pointed out you are using '=' instead of 'eq' eg: (if $type = "regular") instead of (if $type eq "regular"). By using '=' you are actually assigning the value "regular" to the variable '$type' as opposed to testing for its equality to something.



By consequence it will stop at the first "if" test (since $type is being immediately assigned to "regular", it is also immediately true that $type equals "regular") and so no matter what you actually type in for your gas type it will never get beyond the first "if" test for "regular". (i.e. it will never test for equality against "plus", "premium" or "diesel").



Remember that:

$blah = "something" will assign the variable $blah to the string "something".

if ($blah == 45) will test if the variable $blah is equal to the integer 45.

if ($blah eq "something") will test if the variable $blah is equal to the string "something".



A lot of these problems can be avoided by adding two lines to the start of every Perl program you write. A program, especially one written by a beginner, should contain the following lines after the she-bang line (the she-beng line is the very first line #!/usr/bin/perl of every perl script written for unix-like systems, windows systems should also include it):



use strict;

use warnings;



"use strict" forces you to declare all of your variables, this is a good thing and prevents mysterious bugs that can be difficult to trace due to typos in your variable names. "use warnings" will cause the perl compiler to "warn" you about the potential problem your program is experiencing (even though it is not necessarily an error).



If you include "use warnings" to the start of your program, when you execute the script it will display a message such as "Found = in conditional, should be ==".



This will alert you to the fact you need to change the '=' to something else, such as '==' and in your case, it will need to be changed to 'eq' because you are testing for the truth of a string ("something") and not an integer (45).



To repeat what others have said. In order to work properly, your program should resemble something like this:



#!/usr/bin/perl



use strict;

use warnings;



print "Which gas type will you be using?\n";

print "(Regular, Plus, Premium, Diesel)\n";

chomp (my $type = );

if ($type eq "regular") {

# blah blah

}

elsif ($type eq "plus") {

# blah blah

}

# blah blah blah



Good luck!
Jack T
2008-12-03 03:54:38 UTC
Replace your "=" with "eq" in your if statements.

So the first line would become:

if ($type eq "regular") {



Also make sure that you don't confuse lowercase and uppercase. Perl will definitely see the difference.



Why do you write:

$type = $regular;

print "You use regular gas and will pay about \$";

print $type;



Just leave your variables what they are, and change to:

print "You use regular gas and will pay about \$";

print $regular;
B Anne
2008-12-03 03:56:03 UTC
You are using the equal sign when you should be using eq



The equal sign ( = ) assigns the value. eq is used to test for equal.



So:



if ($type = "regular"){

$type = $regular;



should be



if ($type eq "regular"){

$type = $regular;
JComputer
2008-12-03 04:15:43 UTC
In perl, you test sting equality with 'eq' or 'ne' (without quotes).



EX:

if ($type eq "regular) {

...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...