Help needed with basic Perl math commands

Discussion in 'Perl Misc' started by \Dandy\ Randy, Jul 27, 2003.

  1. 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, Jul 27, 2003
    #1
    1. Advertising

  2. "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
    http://www.perldoc.com/perl5.8.0/pod/perlop.html, 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: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jul 27, 2003
    #2
    1. Advertising

  3. "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, Jul 27, 2003
    #3
  4. 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, Jul 27, 2003
    #4
  5. In article <1tGUa.556297$>, "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 - <> - http://www.panix.com/~dha/
    I believe myself to be the daughter of a one-eyed space robot named
    Malcolm. -Fallon Young, http://www.bobbins.org/d/20000915.html
     
    David H. Adler, Jul 27, 2003
    #5
    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. chirs
    Replies:
    18
    Views:
    776
    Chris Uppal
    Mar 2, 2004
  2. AciD_X
    Replies:
    4
    Views:
    8,113
    Jonathan Turkanis
    Apr 1, 2004
  3. Ben Pfaff

    Re: man pages for C commands (GCC commands)

    Ben Pfaff, Jun 24, 2003, in forum: C Programming
    Replies:
    4
    Views:
    3,977
    Thomas Stegen
    Jun 28, 2003
  4. Tim Stanka
    Replies:
    1
    Views:
    807
    Jeff Epler
    Aug 2, 2004
  5. VK
    Replies:
    15
    Views:
    1,182
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page