[Chicago-talk] Use of uninitialized value in printf

Shlomi Fish shlomif at shlomifish.org
Thu Aug 20 00:13:09 PDT 2020


Hi Alan,

On Wed, 19 Aug 2020 16:36:12 -0500
Alan Mead <amead at alanmead.org> wrote:

> I'm trying to use Statistics::Basic to calculate basic statistics like
> correlations. These functions return objects which are supposed to
> magically become numbers as needed, 
> https://metacpan.org/pod/distribution/Statistics-Basic/lib/Statistics/Basic.pod:
> > These actually return objects, not numbers. The objects will
> > interpolate as nicely formated numbers (using Number::Format). Or the
> > actual number will be returned when the object is used as a number.  
> 
> But I have a situation where the magic fails. When one (or both) vector
> has no variance, the correlation should be zero but the value of the
> object is neither defined, nor undefined, it's uninitialized.
> 
> The code is below, here's the output that's driving me crazy. The 0.00
> is correct, but the warnings are annoying:
> 
> $ ./corr.pl
> 1,0.426
> Use of uninitialized value in printf at ./corr.pl line 20.
> 2,0.000
> Use of uninitialized value in addition (+) at ./corr.pl line 25.
> 3,0.000
> 
> Surely there's a way to force an object to be reduced into a scalar
> float (as opposed to just hiding the warnings)?
> 
> I've tried the following:
> 
>   * using scalar() (does nothing);
>   * adding 0 and 0.0 (just moves where the same error is reported);
>   * dereferencing the object and trying to force a scalar with scalar
>     references: "${ $corr }" and "${ $correlation( \@v1, \@v2 ) }"
>     (these are all: Not a SCALAR reference);
>   * checking if $corr is defined (it is, it's a defined object); and
>   * adding ' || 0' to the end of the definition (does nothing).
> 
> I can solve the issue with this:
> 
> $corr = ( defined correlation( \@v1, \@v2 ) ? correlation( \@v1, \@v2 )
> : 0 );
> 

If your perl is recent enough, you can use
https://perldoc.pl/perlop#Logical-Defined-Or :

$corr = correlation(\@v1, \@v2) // 0; # untested

one can also write:

sub _my_dor
{
	my $val = shift;
	return defined($val) ? $val : shift(@_)->();
}

$corr = _my_dor(correlation(\@v1, \@v2), sub { return 0;});

which is kinda ugly.

Also note that there are bindings to GNU R:
https://metacpan.org/search?q=statistics%3A%3Ar as well as
https://metacpan.org/pod/Statistics::Descriptive (which I comaintain) which has
an OOP API.
 



-- 

Shlomi Fish       https://www.shlomifish.org/
Chuck Norris/etc. Facts - https://www.shlomifish.org/humour/bits/facts/

For every A, Chuck Norris is both A and not-A.
Chuck Norris is freaking everything.
    — https://www.shlomifish.org/humour/bits/facts/Chuck-Norris/

Please reply to list if it's a mailing list post - https://shlom.in/reply .


More information about the Chicago-talk mailing list