newby help

Discussion in 'Perl Misc' started by deadmanwalkin, May 22, 2005.

  1. 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
    deadmanwalkin, May 22, 2005
    #1
    1. Advertising

  2. deadmanwalkin

    Peter Wyzl Guest

    "deadmanwalkin" <> wrote in message
    news:...
    :
    : 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
    Peter Wyzl, May 22, 2005
    #2
    1. Advertising

  3. deadmanwalkin <> wrote:

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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, May 22, 2005
    #3
  4. deadmanwalkin wrote:
    > 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.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, May 22, 2005
    #4
  5. * deadmanwalkin schrieb:

    > 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
    Fabian Pilkowski, May 22, 2005
    #5
  6. deadmanwalkin

    Bob Walton Guest

    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
    Bob Walton, May 22, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phrank
    Replies:
    7
    Views:
    414
  2. Rhodri James
    Replies:
    0
    Views:
    676
    Rhodri James
    Feb 24, 2009
  3. Replies:
    1
    Views:
    93
  4. Ron Gunsolus

    Newby Help

    Ron Gunsolus, Aug 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    106
    Phlip
    Aug 20, 2008
  5. Leslie Cox

    newby could use a little help

    Leslie Cox, Nov 22, 2007, in forum: Javascript
    Replies:
    12
    Views:
    189
    Dr J R Stockton
    Nov 30, 2007
Loading...

Share This Page