in reply to returning distance from trendline

#print Dumper length($x_ref);
#print Dumper length($y_ref);

I think it's pretty sad that at this point you still don't know how to find the length of an array. Anyway... Here's your function modified for the info you wanted. But personally I find the idea of having two parallel arrays, one for X and one for Y, to be inelegant. I'd rather have a single array containing "points", where each point would be [ x, y ] . In fact, additional info could be added to each "point", e.g. [ x, y, distance_from_line ] , etc. Then you could think about making a Point object class. But maybe I'm going too fast for you...

sub correlation { my( $x_ref, $y_ref ) = @_; my @x_values = @$x_ref; my @y_values = @$y_ref; my $output = "output_test.txt"; open OUTPUT_TEST, ">$output" or die "can't write $output - $!"; print OUTPUT_TEST Dumper ($x_ref, $y_ref); close OUTPUT_TEST; my $lfit = Statistics::LineFit->new(); $lfit->setData( $x_ref, $y_ref ); my( $intercept, $slope ) = $lfit->coefficients(); my $rSquared = $lfit->rSquared(); my $hashref = { a => $intercept, b => $slope, c => $rSquared }; my( @x_values_for_points_above_the_trend_line, @y_values_for_points_above_the_trend_line, @distances_above_the_trend_line, @x_values_for_points_below_the_trend_line, @y_values_for_points_below_the_trend_line, @distances_below_the_trend_line, @x_values_for_points_on_the_trend_line, @y_values_for_points_on_the_trend_line, ); for ( 0 .. $#x_values ) { my( $x, $y ) = ( $x_values[$_], $y_values[$_] ); my $fx = $intercept + ( $x * $slope ); if ( $y > $fx ) { push @x_values_for_points_above_the_trend_line, $x; push @y_values_for_points_above_the_trend_line, $y; push @distances_above_trend_line, $y - $fx; } elsif ( $y < $fx ) { push @x_values_for_points_below_the_trend_line, $x; push @y_values_for_points_below_the_trend_line, $y; push @distances_below_trend_line, $fx - $y; } else { push @x_values_for_points_on_the_trend_line, $x; push @y_values_for_points_on_the_trend_line, $y; } } return( $hashref, \@x_values_for_points_above_the_trend_line, \@y_values_for_points_above_the_trend_line, \@distances_above_the_trend_line, \@x_values_for_points_below_the_trend_line, \@y_values_for_points_below_the_trend_line, \@distances_below_the_trend_line, \@x_values_for_points_on_the_trend_line, \@y_values_for_points_on_the_trend_line, ); }

(Disclaimer: Code Untested.)

A word spoken in Mind will reach its own level, in the objective world, by its own weight