That usage of map has the effect of passing a list of values to your "sum" function, but you have written "sub sum" to expect an array reference, and the error arises when you try to dereference the first arg to sum().return (sum map { ($_ - $mean)**2 } @_) / $#_;
It if you really want to pass an array reference to sum(), put square brackets around the map expression:
But then you'll discover another problem: since variance() is being passed an array ref (always just one arg), the the expression $#_ will always be zero, so it'll die with a "divide by zero" error. You need to divide by the number of elements in the array that is referenced by the first element of @_, and you also need to dereference that element to get the input list for map:return (sum [map { ($_ - $mean)**2 } @_]) / $#_;
I'm kind of rusty with my statistics arithmetic -- maybe you actually want the number of array elements minus 1? That would be $#{$_[0]}.return (sum [ map { ($_ - $mean)**2 } @{$_[0]} ] ) / @{$_[0]};
In reply to Re: variance calculation
by graff
in thread variance calculation
by cdfd123
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |