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

Re: Help needed with basic Perl math commands

"Dandy" Randy wrote: Quote:

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

Re: Help needed with basic Perl math commands

"Dandy" Randy wrote: Quote:

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

Quote:

> I will now split the data like this:

> chomp ($list);

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

Look good, still.

Quote:

> 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

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

Quote:

Quote:

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

>

>

Re: Help needed with basic Perl math commands

In article <1tGUa.556297$Vi5.13989964news1.calgary.shaw.ca >, "Dandy"

Randy wrote:

Quote:

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