perl - Multiply each element of an array to each other -


i have array of float numbers: (.25, .45, .15, .27). multiply each element each other divide number of array elements.

foreach $element (@array) { $score = $score * $element; } $score = $score/$numofelements; 

this produces value 0. not sure if syntax correct.

your syntax correct, initializing $score variable each time through loop, sets 0 each iteration. move outside loop , should work:

my $score = 1 #need initialize 1 multiplying foreach $element (@array) {     $score *= $element; } $score = $score/$numofelements; #assuming $numofelements set 

Comments

Popular posts from this blog

java - How to Configure JAXRS and Spring With Annotations -

visual studio - TFS will not accept changes I've made to a Java project -

php - Create image in codeigniter on the fly -