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

Dear Monks,

Why does this line:
my (%hashref_return_A) = correlation(\@primarys_array, \@secondarys_ +array);
give me this warning:
Reference found where even-sized list expected at script.pl line 576
The subroutine is taking the request like:
sub correlation { my ($x_ref, $y_ref) = @_; my $lfit = Statistics::LineFit->new(); $lfit->setData($x_ref, $y_ref);

Replies are listed 'Best First'.
Re: Reference found where even-sized list expected
by moritz (Cardinal) on Nov 29, 2007 at 12:03 UTC
    It looks like the sub returns a hash ref, and you try to store it in a hash.

    There are two ways around it

    # 1) store it in a reference: my $hashref = correlation(\@primarys_array, @secondarys_array); # 2) deference it and store it in a hash: my %hash = %{ correlation(\@primarys_array, @secondarys_array) };

    I recommend the first one, though.

    See perlreftut for more details.

Re: Reference found where even-sized list expected
by jhourcle (Prior) on Nov 29, 2007 at 12:08 UTC

    What is returned from correlation (or the last statement in correlation, if you don't specifically return a value?

    From the error given, I'm going to assume it's a reference of some form ... however, when declaring a hash in the way that you have, you need to specify a list of pairs (ie, an even sized list), rather than a reference.

    Just for debugging, you might want to examine what's being returned from correlation:

    my @return = correlation(\@primarys_array, \@secondarys_array); use Data::Dumper; warn Dumper \@return;

    As you've named the variable 'hashref_return_A', I'm going to assume the return is a hashref, and not a hash. In which case, you'll want to use one of the following:

    # note -- it's a scalar ($) not a hash (%) my $hashref_return_A = correlation(\@primarys_array, \@secondarys_arr +ay); # or # %{ ... } casts a hashref to a list my (%hashref_return_A) = %{ correlation(\@primarys_array, \@secondarys +_array) };