Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help needed with basic Perl math commands

Reply
Thread Tools

Help needed with basic Perl math commands

 
 
\Dandy\ Randy
Guest
Posts: n/a
 
      07-26-2003
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


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-27-2003
"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

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      07-27-2003
"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


 
Reply With Quote
 
\Dandy\ Randy
Guest
Posts: n/a
 
      07-27-2003
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
>
>



 
Reply With Quote
 
David H. Adler
Guest
Posts: n/a
 
      07-27-2003
In article <1tGUa.556297$(E-Mail Removed) >, "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 - <(E-Mail Removed)> - 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
math.h trig functions questions (and some forgotten high school math) Mark Healey C Programming 7 05-22-2006 10:42 AM
Need Help Differentiating Bad Commands From Incomplete Commands Tim Stanka Python 1 08-02-2004 02:08 AM
Re: Is still math.h the C++ math library ? AciD_X C++ 4 04-01-2004 07:29 PM
Why can I not use: Math a=new Math(); chirs Java 18 03-02-2004 06:00 PM



Advertisments