Gossamer Forum
Home : General : Perl Programming :

Percentage

Quote Reply
Percentage
This is sort of hard to explain.
I have lets say 6 ratings for a certain link in a database. I'd like to get all the ratings and do whatever it is that perl can do to get the percentage of ratings 4 and up to ratings 3 and below. So like one rating is 1 and the rest are 5's. I'd assume that since there are only 6 links it'd be somewhere around 5% bad 95% good. Can anyone understand Smile/help me with this?
Lavon Russell
LookHard Mods
lavon@lh.links247.net
Quote Reply
Re: [Bmxer] Percentage In reply to
Not too good at the math but i was just thinking all I need to do is divide the rating of 1 by the total of ratings 6, and then move the decimal place over.

And the same for the positive 5/6, does this sound right?
Lavon Russell
LookHard Mods
lavon@lh.links247.net
Quote Reply
Re: [Bmxer] Percentage In reply to
If you got everything into an array you could do:

Code:
my (@array) = qw( 3 4 7 1 4 1 2); #example
my ($high,$low);
my ($total) = scalar @array;

for (@array) {
if ($_ > 3) { $high++ } elsif ($_ < 4) { $low++ }
}

Then you can just do something like:

my $high_percent = sprintf("%.2f", (($high / $total) * 100));
my $low_percent = sprintf("%.2f", (($low / $total) * 100));

I _think_ that should work. You should end up with percentages for how many ratings are above/below 3.

There may be an easier way :)


Last edited by:

RedRum: Jan 6, 2002, 11:02 AM
Quote Reply
Re: [RedRum] Percentage In reply to
That did it. I didn't use the array part because I was already doing a while loop so i just did something like

Code:


($values[$db_op_pid] eq $id) and $numratings++;


($values[$db_op_pid] eq $id) and ($values[$db_op_rating] >= 4) and $preapprove++;
($values[$db_op_pid] eq $id) and ($values[$db_op_rating] <= 3) and $predisapprove++;


and then while the total of ratings for that link is in $numratings, using what you just gave me:

Code:
$approve = sprintf("%.0f", (($preapprove / $numratings) * 100));
$disapprove = sprintf("%.0f", (($predisapprove / $numratings) * 100));


Right before I read your solution I was trying it without the 100's at the end. Like I said I'm not great at math when it comes to algebra type stuff and percentages.

Thanks alot Paul.


Lavon Russell
LookHard Mods
lavon@lh.links247.net
Quote Reply
Re: [Bmxer] Percentage In reply to
To get an average, most likely i'll have to push all ratings for the link into and array while counting how many i put in and add them, then divide by the number right?

I tried a way of getting them into an array but I don't think it's working.

Code:
($values[$db_op_pid] eq $id) and (push (@average, $values[$db_op_rating])) and ($preaverage++);

Lavon Russell
LookHard Mods
lavon@lh.links247.net
Quote Reply
Re: [Bmxer] Percentage In reply to
How about:

Code:
if ($values[$db_op_pid] == $id) {
$count += $values[$db_op_rating]));
$preaverage++;
}

my $ave = $count / $preaverage;

Is that what you need?


(Notice you need to use == not eq)

Last edited by:

RedRum: Jan 6, 2002, 11:37 AM
Quote Reply
Re: [RedRum] Percentage In reply to
 
Quote:
(Notice you need to use == not eq)
whoops... Don't know why I was using eq. Anyway, thank you, it totals them all fine, and I get 1.6666667 and all that. When I use int, shouldn't it round up to 2? It goes to 1.
Lavon Russell
LookHard Mods
lavon@lh.links247.net
Quote Reply
Re: [Bmxer] Percentage In reply to
int rounds down and shouldn't be used for rounding up (lol obviously). You'd need to use POSIX::ceil for that or sprintf/printf



Last edited by:

RedRum: Jan 6, 2002, 12:02 PM
Quote Reply
Re: [RedRum] Percentage In reply to
Ok, got it. i thought I saw int round up before, and that's why I was trying with it, but I guess maybe what I was it working with before was sprintf. anyway, thanks
Lavon Russell
LookHard Mods
lavon@lh.links247.net