Perl math problem..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • WipeOut

    Perl math problem..

    I am having a problem in a perl script that I can't seem to find an
    answer for..

    The $cost and $retail vars come from another part of the script and
    would be something like 000134.345 and 000220.202 respectively..

    I an then trying to use these values in a calculation like this..

    $profit = ($retail - $cost) / $retail * 100;

    But I get the following error..

    ../avgprofit.pl PriceList.txt
    Illegal division by zero at ./avgprofit.pl line 35, <> line 1.

    If is substitute the values directly into the calculation it works
    perfectly..

    Anyone got any ideas?

    Thanks..
  • Daniel Moree

    #2
    Re: Perl math problem..

    WipeOut wrote:[color=blue]
    > I am having a problem in a perl script that I can't seem to find an
    > answer for..
    >
    > The $cost and $retail vars come from another part of the script and
    > would be something like 000134.345 and 000220.202 respectively..
    >
    > I an then trying to use these values in a calculation like this..
    >
    > $profit = ($retail - $cost) / $retail * 100;
    >
    > But I get the following error..
    >
    > ./avgprofit.pl PriceList.txt
    > Illegal division by zero at ./avgprofit.pl line 35, <> line 1.
    >
    > If is substitute the values directly into the calculation it works
    > perfectly..
    >
    > Anyone got any ideas?
    >
    > Thanks..[/color]

    I'd help, but i'd need to see the rest of the file.

    What the problem mostlikely is, is that the variable $retail and $cost
    were initialized inside a sub or such. Try putting at the very top of
    your perl file something like this.

    my $retail = 0;
    my $cost = 0;

    if you need to change some stuff do so. But it seems like it's not
    reading it as a global but a local variable.

    Daniel

    Comment

    • WipeOut

      #3
      Re: Perl math problem..

      Daniel Moree wrote:[color=blue]
      > WipeOut wrote:
      >[color=green]
      >> I am having a problem in a perl script that I can't seem to find an
      >> answer for..
      >>
      >> The $cost and $retail vars come from another part of the script and
      >> would be something like 000134.345 and 000220.202 respectively..
      >>
      >> I an then trying to use these values in a calculation like this..
      >>
      >> $profit = ($retail - $cost) / $retail * 100;
      >>
      >> But I get the following error..
      >>
      >> ./avgprofit.pl PriceList.txt
      >> Illegal division by zero at ./avgprofit.pl line 35, <> line 1.
      >>
      >> If is substitute the values directly into the calculation it works
      >> perfectly..
      >>
      >> Anyone got any ideas?
      >>
      >> Thanks..[/color]
      >
      >
      > I'd help, but i'd need to see the rest of the file.
      >
      > What the problem mostlikely is, is that the variable $retail and $cost
      > were initialized inside a sub or such. Try putting at the very top of
      > your perl file something like this.
      >
      > my $retail = 0;
      > my $cost = 0;
      >
      > if you need to change some stuff do so. But it seems like it's not
      > reading it as a global but a local variable.
      >
      > Daniel[/color]

      Hi Daniel..

      Its a very simple script for taking a fixed length database file and
      creating a CSV created from my limited perl ability.. :)

      Thanks for your help..

      Here is the whole script (may be some line wrapping)..

      #!/usr/bin/perl

      #Set up first line field desc.
      #print "\"PRODUCTCODE\ ";\"PRODUCT\";\ "COST_PRICE\";\ "PRICE\";\"PROF IT\";\n";

      # Read from standard input or file specified on command line.
      while (<>) {

      # Specify each field as a or A with its width.
      # Use a to preserve trailing spaces, A to strip them.
      @fields = unpack('A18 A30 A1 A9 A9 A3 A2 A18 A2 A11 A2 A9 A3 A9 A3 A9
      A3 A1 A4 A7 A1 A1', $_);

      # Insert decimal point into price fields.
      $fields[3] =~ s/(...)$/.$1/;
      $fields[4] =~ s/(...)$/.$1/;

      # Clean up and substitutions.
      foreach $f (@fields) {
      # Replace \'s with /'s
      $f =~ s/(\\)/\//g;
      # Remove leading spaces
      $f =~ s/^\s//;
      # Double any " characters, and then enclose in "
      $f =~ s/"/""/g;
      }

      # Profit calculation.
      $cost = $fields[3];
      $retail = $fields[4];
      $profit = ($retail - $cost) / $retail * 100;
      #$profit = (220.202 - 000134.567) / 220.202 * 100;

      # Print out seperated list except where price field is less than
      000005.000 (zero).
      if ($fields[4] gt "000005.000 ") {
      # "PRODUCTCODE";" PRODUCT";"COST_ PRICE";"PRICE;" PROFIT"
      print "$fields[0];$fields[1];$cost;$retail; $profit;\n";
      }
      }

      #end

      Comment

      • Daniel Moree

        #4
        Re: Perl math problem..

        # Profit calculation.
        $cost = $fields[3];
        $retail = $fields[4];
        $profit = ($retail - $cost) / $retail * 100;
        #$profit = (220.202 - 000134.567) / 220.202 * 100;

        This section looks good, I'd try something like this just to see what
        info is actually in $cost and $retail try this

        print "FIELDS[3]: $fields[3]\n";
        print "FIELDS[4]: $fields[4]\n";
        $cost = $fields[3];
        $retail = $fields[4];
        $profit = ($retail - $cost) / $retail * 100;
        #$profit = (220.202 - 000134.567) / 220.202 * 100;

        this should print out what's in the fields array sections. The unpack
        could cause no data to enter the array. Just check what's really in
        those fields, if nothing, then check the data input($_), if there is
        data in the input, then the unpack function isn't working right

        Daniel

        Comment

        • WipeOut

          #5
          Re: Perl math problem..

          Daniel Moree wrote:[color=blue]
          > # Profit calculation.
          > $cost = $fields[3];
          > $retail = $fields[4];
          > $profit = ($retail - $cost) / $retail * 100;
          > #$profit = (220.202 - 000134.567) / 220.202 * 100;
          >
          > This section looks good, I'd try something like this just to see what
          > info is actually in $cost and $retail try this
          >
          > print "FIELDS[3]: $fields[3]\n";
          > print "FIELDS[4]: $fields[4]\n";
          > $cost = $fields[3];
          > $retail = $fields[4];
          > $profit = ($retail - $cost) / $retail * 100;
          > #$profit = (220.202 - 000134.567) / 220.202 * 100;
          >
          > this should print out what's in the fields array sections. The unpack
          > could cause no data to enter the array. Just check what's really in
          > those fields, if nothing, then check the data input($_), if there is
          > data in the input, then the unpack function isn't working right
          >
          > Daniel[/color]

          I use the $cost and $retail vars further down when "print"ing the line
          to create the CSV format output and they are working fine..

          Thats why it is so strange that the calculation doesn't work with the
          vars but when a value is used in place of it that is a similar value to
          the one contained in the var it doesn't work..

          Also I tried braking it down..

          $profit = ($retail - $cost); #this works

          $profit = ($retail - $cost) / $retail; #this doesn't

          ...so something to do with the division is broken when using a var.. :(


          Comment

          • Joe Smith

            #6
            Re: Perl math problem..

            WipeOut wrote:
            [color=blue]
            > Also I tried braking it down..
            >
            > $profit = ($retail - $cost); #this works
            >
            > $profit = ($retail - $cost) / $retail; #this doesn't
            >
            > ..so something to do with the division is broken when using a var.. :([/color]

            How can you say that?
            You haven't proven to us that $retail _isn't_ zero.

            warn "Error: \$retail is not defined" unless defined $retail;
            warn "Error: \$retail is zero" if $retail == 0;
            $profit = ($retail - $cost) / $retail;

            -Joe

            Comment

            • WipeOut

              #7
              Re: Perl math problem..

              Joe Smith wrote:[color=blue]
              > WipeOut wrote:
              >[color=green]
              >> Also I tried braking it down..
              >>
              >> $profit = ($retail - $cost); #this works
              >>
              >> $profit = ($retail - $cost) / $retail; #this doesn't
              >>
              >> ..so something to do with the division is broken when using a var.. :([/color]
              >
              >
              > How can you say that?
              > You haven't proven to us that $retail _isn't_ zero.
              >
              > warn "Error: \$retail is not defined" unless defined $retail;
              > warn "Error: \$retail is zero" if $retail == 0;
              > $profit = ($retail - $cost) / $retail;
              >
              > -Joe[/color]
              Cracked it.. :)

              Basically this script is a manipulation of another one I wrote and what
              I had overlooked is that I have an "if" statement further down to ignore
              anything with a value less than 000005.000, which would include zero's..

              I moved the calculation below that and it works fine now..

              Just another example of the error being between the keyboard and the
              seat.. :)

              Daniel and Joe, thanks for you help..

              Comment

              Working...