deadmanwalkin wrote:
> 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
> {
> $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
>
--
Bob Walton
Email:
http://bwalton.com/cgi-bin/emailbob.pl