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 - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -