# newby help

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

i am bringing the variable \$plant across from another html script and
trying to asign a price to the price variable buy using a if statment.
it only ever assigns the value of \$price to the first value in the if
statment, or the last value in the if statment.

i would apreciate any help you can give asap.
below is script im using please let me know if it can be improved

im not looking for some one to fix if for me im looking for pointers in
the right direction.

code starts here

\$VAT=17.5;

if (\$Plant=="Japanese Anemones")
{
\$Price= 7.90;
}

elsif (\$Plant=="Bird of Paradise")
{
\$Price= 5.90;
}

elsif (\$Plant=="Flowering Geraniums")
{
\$Price= 15.90;
}

elsif (\$Plant=="Rose of Sharon")
{
\$Price= 10.90;
}

\$subttl=\$Price*\$VAT/100;
\$total=\$Price+\$subttl;
printf "total including Vat is  %9.2f\n",\$total;

code end

## Re: newby help

Please put the subject of your article in the Subject of your article.

HTML is not a programming language, you cannot make scripts with it,
nor does it have variables...

^^^^
^^^^
We take our time here on Usenet. It your problem is time-critical,
consider hiring someone who knows Perl to solve it for you.

You should always enable warnings when developing Perl code!

It would have found your bug straightaway...

Argument "Japanese Anemones" isn't numeric in numeric eq (==) at ...

Since you are using a numeric comparison operator, both operands
will be converted from strings to numbers.

\$Plant likely converts to zero.

"Japanese Anemones" definitely converts to zero.

The if body will execute if zero equals zero.

Use "eq" to compare strings, use "==" to compare numbers.

--
Tad McClellan                          SGML consulting
Fort Worth, Texas

## Re: newby help

:
: if (\$Plant=="Japanese Anemones")
: {
: \$Price= 7.90;
: }

numeric comparison operator is '=='

string comparison operator is 'eq'

You are comparing strings...

if (\$Plant eq "Japanese Anemones"){
\$Price= 7.90;
}

P

## Re: newby help

What's an "html script"?

You are asking for free help on Usenet. Don't expect anybody to care
whether you are in a hurry.

Enable strictures and warnings, and declare the variables.

use strict;
use warnings;

With warnings enabled, Perl would have told you that you are using the
wrong comparison operator, and you wouldn't have needed to unnecessarily
ask others for help.

--
Email: http://www.gunnar.cc/cgi-bin/contact.pl

## Re: newby help

You want to use a hash instead of numerous if statements (with a wrong
comparison operator as other already mentioned).

Well, you could declare that hash like this:

my %prices = (
'Japanese Anemones'   =>  7.90,
'Bird of Paradise'    =>  5.90,
'Flowering Geraniums' => 15.90,
'Rose of Sharon'      => 10.90,
);

While reading `perldoc perlintro` (esp. the paragraph "Perl variable
types") you should learn how to access its data as you like.

regards,
fabian

## Re: newby help

HTML doesn't have scripts or variables.  Please clarify what you

if statements don't assign values to variables.

-------------------------------------^
You mention variable \$price here and you show variable \$Price in
your code below.  Which is it?  Perl variable names are case
sensitive.

use strict;  #always use these, at least during development
use warnings;#

As many others have mentioned, you need "eq" not "==" here.
In addition, you should have an "else" clause for your if
statement in the event \$Plant isn't one of the listed
alternatives.  And, of course, all this stuff should probably be
obtained from a database or a data file, as you won't want to
have to change your Perl code every time a price changes.  A hash
would be the proper mechanism to implement this efficiently and
clearly.  Something along the lines of:

use strict;
use warnings;
my %hash=(
'Bird of Paradise'   => 7.90,
'Flowering Geranims' =>15.90,
'Rose of Sharon'     =>10.90,
);
my \$VAT=17.90;
while(my \$Plant=<DATA>){
chomp \$Plant;
printf "total including Vat is  %9.2f\n",
\$hash*(1+\$VAT/100)
if exists \$hash;
}
__END__