CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Summing up the values of string?

 



Baris
Novice

Jul 1, 2002, 6:59 PM

Post #1 of 4 (697 views)
Summing up the values of string? Can't Post

I've a variable which is consisting of votes (1-10) which are separated by \n. I would like to sum up all this votes and divide to the number of all votes to calculate the rating. Can anyone help me how to do it?

e.g.

$votes = "5\n 1\n 6\n 3\n 5\n";

I know how to get the length of votes:

@length_votes = split(/\n/, $sonuc);

(It would show 5)

$rating = $all_votes / @length_votes;

(It should show 20/5= 4= $rating)

How can I calculate $all_votes?

I hope anyone can help me, thanks.


Baris
Novice

Jul 1, 2002, 7:06 PM

Post #2 of 4 (696 views)
Re: [Baris] Summing up the values of string? [In reply to] Can't Post

Sorry, there was an error,

@length_votes = split(/\n/, $sonuc);

should be

@length_votes = split(/\n/, $votes);


davorg
Thaumaturge / Moderator

Jul 1, 2002, 10:43 PM

Post #3 of 4 (692 views)
Re: [Baris] Summing up the values of string? [In reply to] Can't Post


Code
my @votes = split /\n/, $votes; 

my $total;

$total += $_ foreach @votes;

my $avg = $total / @votes;


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Baris
Novice

Jul 2, 2002, 2:05 AM

Post #4 of 4 (690 views)
Re: [davorg] Summing up the values of string? [In reply to] Can't Post

Thanks davorg.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives