Professional Web Applications Themes

Help needed with basic Perl math commands - PERL Miscellaneous

Hey everyone, I'm writting a simple script that takes data from a text file and calculates percentages. Lets call this file "data.txt" In data.txt, there are some numbers separated by "|" symbols like below: 2000|1800|500 I will open the file and get its data like this: open (list, "<data.txt") or &error("Unable to open the data file for reading"); $list=<list>; close(list); I will now split the data like this: chomp ($list); ($total,$bought,$sold)=split(/\|/,$list); Now I need help writting a set of commands that will calculate the percentages of this data. I would like to have it in this format: print "Total was ...

  1. #1

    Default Help needed with basic Perl math commands

    Hey everyone, I'm writting a simple script that takes data from a text file
    and calculates percentages. Lets call this file "data.txt" In data.txt,
    there are some numbers separated by "|" symbols like below:

    2000|1800|500

    I will open the file and get its data like this:

    open (list, "<data.txt") or &error("Unable to open the data file for
    reading");
    $list=<list>;
    close(list);

    I will now split the data like this:

    chomp ($list);
    ($total,$bought,$sold)=split(/\|/,$list);

    Now I need help writting a set of commands that will calculate the
    percentages of this data. I would like to have it in this format:

    print "Total was $total";
    print "Bought was $boughtpercentage"; # % of $bought vs $total
    print "Sold was $soldpercentage"; # % of $sold vs $total

    Can someone please help me with the commands to calculate $boughtpercentage
    & $soldpercentage. Thanx to all who read or respond. Code examples are
    greatly appreciated. TIA!!!

    R


    \Dandy\ Randy Guest

  2. #2

    Default Re: Help needed with basic Perl math commands

    "Dandy" Randy wrote:
    > Now I need help writting a set of commands that will calculate the
    > percentages of this data. I would like to have it in this format:
    >
    > print "Total was $total";
    > print "Bought was $boughtpercentage"; # % of $bought vs $total
    > print "Sold was $soldpercentage"; # % of $sold vs $total
    >
    > Can someone please help me with the commands to calculate
    > $boughtpercentage & $soldpercentage.
    I'm sure someone can. The question is if someone wants.

    You'd better study the relevant docs, such as
    [url]http://www.perldoc.com/perl5.8.0/pod/perlop.html[/url], at give it a try by
    yourself.

    Then, if you - contrary to expectation - would encounter difficulties,
    give us some proof that you actually made an own effort. That will
    increase your chances to get help here.

    --
    Gunnar Hjalmarsson
    Email: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]

    Gunnar Hjalmarsson Guest

  3. #3

    Default Re: Help needed with basic Perl math commands

    "Dandy" Randy wrote:
    > Hey everyone, I'm writting a simple script that takes data from a
    > text file and calculates percentages. Lets call this file "data.txt"
    > In data.txt, there are some numbers separated by "|" symbols like
    > below:
    >
    > 2000|1800|500
    >
    > I will open the file and get its data like this:
    >
    > open (list, "<data.txt") or &error("Unable to open the data file for
    > reading");
    > $list=<list>;
    > close(list);
    So far so good.
    > I will now split the data like this:
    > chomp ($list);
    > ($total,$bought,$sold)=split(/\|/,$list);
    Look good, still.
    > Now I need help writting a set of commands that will calculate the
    > percentages of this data. I would like to have it in this format:
    >
    > print "Total was $total";
    > print "Bought was $boughtpercentage"; # % of $bought vs $total
    > print "Sold was $soldpercentage"; # % of $sold vs $total
    >
    > Can someone please help me with the commands to calculate
    > $boughtpercentage & $soldpercentage. Thanx to all who read or
    > respond.
    Oh come on. That is second or at most third grade math!
    How would you do that with paper and pencil? Write it down. Explicitly!
    And then just type that formula into your computer.

    For the actual syntax check 'perldoc perlop' for the Perl operators.
    Hint: multiplication is "*" and division is "/".

    jue


    Jürgen Exner Guest

  4. #4

    Default Re: Help needed with basic Perl math commands

    Jue, first off thanx for the reply. I, of course, know how to do the math on
    paper, but am unfamiliar with the perl syntax to complete such a function.
    How do I tell Perl that the information gathered from the text file are
    integers and not strings ... by this I mean ... what if the text file had
    words and not numbers ... then I tried calculating a percentage from words
    .... see where I'm going? Maybe I'm thinking too hard and perl will just
    figure out that the data are numbers and not words ... if you have any input
    on this matter, you are most welcomed to reply. TIA

    R
    > > Now I need help writting a set of commands that will calculate the
    > > percentages of this data. I would like to have it in this format:
    > >
    > > print "Total was $total";
    > > print "Bought was $boughtpercentage"; # % of $bought vs $total
    > > print "Sold was $soldpercentage"; # % of $sold vs $total
    > >
    > > Can someone please help me with the commands to calculate
    > > $boughtpercentage & $soldpercentage. Thanx to all who read or
    > > respond.
    >
    > Oh come on. That is second or at most third grade math!
    > How would you do that with paper and pencil? Write it down. Explicitly!
    > And then just type that formula into your computer.
    >
    > For the actual syntax check 'perldoc perlop' for the Perl operators.
    > Hint: multiplication is "*" and division is "/".
    >
    > jue
    >
    >

    \Dandy\ Randy Guest

  5. #5

    Default Re: Help needed with basic Perl math commands

    In article <1tGUa.556297$Vi5.13989964news1.calgary.shaw.ca >, "Dandy"
    Randy wrote:
    > Jue, first off thanx for the reply. I, of course, know how to do the math on
    > paper, but am unfamiliar with the perl syntax to complete such a function.
    > How do I tell Perl that the information gathered from the text file are
    > integers and not strings ... by this I mean ... what if the text file had
    > words and not numbers ... then I tried calculating a percentage from words
    > ... see where I'm going? Maybe I'm thinking too hard and perl will just
    > figure out that the data are numbers and not words ...
    Unless you mean words such as "one", "two", "three" etc., yes, perl will
    figure it out.

    If a variable contains the string "1" it will convert it to a number
    if necessary if it's used in a mathematical context. And similarly the
    other way for a string context.

    I'm sure someone will pick at this, as I'm probably simplifying
    horribly, but it should be enough to deal with this particular problem.
    :-)

    dha

    --
    David H. Adler - <dhapanix.com> - [url]http://www.panix.com/~dha/[/url]
    I believe myself to be the daughter of a one-eyed space robot named
    Malcolm. -Fallon Young, [url]http://www.bobbins.org/d/20000915.html[/url]
    David H. Adler Guest

Similar Threads

  1. CF 6.1 is wrong when performing BASIC math operations!!
    By LuigiPia in forum Coldfusion - Advanced Techniques
    Replies: 4
    Last Post: March 24th, 07:40 PM
  2. perl interactive system commands
    By Andrew Gaffney in forum PERL Beginners
    Replies: 1
    Last Post: November 27th, 07:13 PM
  3. Math Help Needed
    By Ralph Freshour in forum PHP Development
    Replies: 2
    Last Post: November 15th, 06:42 AM
  4. Perl Math Syntax
    By \Dandy\ Randy in forum PERL Miscellaneous
    Replies: 21
    Last Post: August 11th, 01:06 PM
  5. HELP! Needed with Fox Pro date math
    By Dave Navarro in forum ASP Database
    Replies: 3
    Last Post: August 11th, 10:34 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139