newby help

D

deadmanwalkin

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
 
P

Peter Wyzl

:
: 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
 
T

Tad McClellan

deadmanwalkin said:
Subject: newby help


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

i am bringing the variable $plant across from another html script and


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

i would apreciate any help you can give asap.
^^^^
^^^^
We take our time here on Usenet. It your problem is time-critical,
consider hiring someone who knows Perl to solve it for you.

looking for pointers in
the right direction.


You should always enable warnings when developing Perl code!

It would have found your bug straightaway...

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


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.
 
G

Gunnar Hjalmarsson

deadmanwalkin said:
i am bringing the variable $plant across from another html script

What's an "html script"?
i would apreciate any help you can give asap.

You are asking for free help on Usenet. Don't expect anybody to care
whether you are in a hurry.
below is script im using please let me know if it can be improved

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.
 
F

Fabian Pilkowski

* deadmanwalkin said:
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

You want to use a hash instead of numerous if statements (with a wrong
comparison operator as other already mentioned).
im not looking for some one to fix if for me im looking for pointers in
the right direction.

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
 
B

Bob Walton

deadmanwalkin said:
i am bringing the variable $plant across from another html script and

HTML doesn't have scripts or variables. Please clarify what you
are talking about.
trying to asign a price to the price variable buy using a if statment.

if statements don't assign values to variables.
it only ever assigns the value of $price to the first value in the if
-------------------------------------^
You mention variable $price here and you show variable $Price in
your code below. Which is it? Perl variable names are case
sensitive.
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

use strict; #always use these, at least during development
use warnings;#
$VAT=17.5;

if ($Plant=="Japanese Anemones")
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{$Plant}*(1+$VAT/100)
if exists $hash{$Plant};
}
__END__
Bird of Paradise
Non Existent
Rose of Sharon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top