Help needed with basic Perl math commands

D

\Dandy\ Randy

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
 
G

Gunnar Hjalmarsson

Dandy" Randy said:
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.
 
J

Jürgen Exner

Dandy" Randy said:
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
 
D

\Dandy\ Randy

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
 
D

David H. Adler

"Dandy" said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top