Win has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have a subroutine like:
sub correlation { print "\nWe are in the correlation subroutine\n\n"; my ($x_ref, $y_ref) = @_; my @x = @{$x_ref}; my @y = @{$y_ref}; my $lfit = Statistics::LineFit->new(); $lfit->setData(@x, @y); printf "a=%.4f, b=%.4f, R2=%.4f\n", $lfit->coefficients(), $lfit->r +Squared(); return "a=%.4f, b=%.4f, R2=%.4f\n", $lfit->coefficients(), $lfit->r +Squared(); }
This sub is activated like:
my ($y_axis_intercept, $gradient, $R2) = correlation(\@primarys, \@sec +ondarys);
The error message that I get is:
Can't use an undefined value as an ARRAY reference at c:/Perl/site/lib +/Statistic s/LineFit.pm line 191, <DATA> line 164.

Replies are listed 'Best First'.
Re: LineFit.pm error due to undef value
by jdporter (Paladin) on Nov 20, 2007 at 15:44 UTC

    RTFM!

    $lineFit->setData(\@x, \@y)

    Explaining why it should be like this is something probably best left to the tutorials.

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

      I'll add (thought I expect this will go right over your head) that you don't even need to deref and reref the arrays:

      sub correlation { . . . my( $x_ref, $y_ref ) = @_; my $lfit = Statistics::LineFit->new(); $lfit->setData( $x_ref, $y_ref ); . . . }
      A reply falls below the community's threshold of quality. You may see it by logging in.