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

Hi,

I made a perl mod called regression that takes a pointer to a hash of x,y data pairs when it is blessed. I tested it with a simple program and verified that it runs with that program. Here is part of the simple program code…

my %raw_data = ( 1 => 100, 2 => 160, 2.5 => 182, 3 => 225 ); my $regr = regression->new(\%raw_data); print '$regr= ', Dumper($regr); $mean_x = $regr->get_mean_x; print '$mean_x= ', Dumper($mean_x);


and here is some of the output. (The numbers are all correct.)

$regr= $VAR1 = bless( { 'mean_y' => '166.75', 'raw_data' => { '1' => 100, '3' => 225, '2' => 160, '2.5' => 182 }, 'slope' => '60.6285714285714', 'mean_x' => '2.125', 'y_intercept' => '37.9142857142857' }, 'regression' );


Well, I try it with a work example, which requires a more complicated hash structure.

Here is where the hash is populated:

$raw_dataPtr = \%{$element_stats_hash{$element_name}{'raw_data +'}}; $$raw_dataPtr{$time} = {$var};


So, the hash key is set to the values in variable $time and the hash value is set to values in variable $var.

Here is where the object is blessed into existence:

#$regr = regression->new(\%{$element_stats_hash{$element_name}{'ra +w_data'}}); $regr = regression->new(\%{$element_stats_hash{$element_name}}); print TEST '$regr= ', Dumper($regr);


I have also tried this:

$regr = regression->new(\%{$element_stats_hash{$element_name}}); print TEST '$regr= ', Dumper($regr);


Here is the output of the class’s hash data structure (either way):

'raw_data' => { '1229265435' => { '0. +12205589' => undef }, '1229299036' => { '0. +71940041' => undef }, '1229317107' => { '1. +22392523' => undef },


Of course, I WANT it to look like this:

'raw_data' => { '1229265435' => '0.12 +205589', '1229299036' => '0.71 +940041', '1229317107' => '1.2239252 +3' },


and I just cannot seem to understand what is going on here. Might someone be able to explain to me what I need to do in order to get the hash in the class to look like I want it to? And can someone explain what is presently going on?

Many thanks in advance.

Tony

Replies are listed 'Best First'.
Re: Confounded With A Simple Self-Made Perl Mod
by GrandFather (Saint) on Dec 15, 2008 at 23:18 UTC

    I suspect you want:

    $regr = regression->new($element_stats_hash{$element_name});

    which I'm guessing is already a reference to a hash of your raw data.

    It looks like you are going out of your way to make things complicated by wrapping \%{} around things that are already hash references. For example:

    $raw_dataPtr = \%{$element_stats_hash{$element_name}{'raw_data'}}; $$raw_dataPtr{$time} = {$var};

    would be much better as:

    $element_stats_hash{$element_name}{'raw_data'}{$time} = {$var};

    except that {$var} is wrong in any case. You really must use strictures (use strict; use warnings;)! With strictures that line would generate something like:

    Odd number of elements in anonymous hash at noname.pl line 8.

    Perl's payment curve coincides with its learning curve.
Re: Confounded With A Simple Self-Made Perl Mod
by almut (Canon) on Dec 15, 2008 at 23:17 UTC
    $raw_dataPtr = \%{$element_stats_hash{$element_name}{'raw_data'}}; $$raw_dataPtr{$time} = {$var};

    I think you want something like this:

    #!/usr/bin/perl use Data::Dumper; my $time = time(); my $val = 0.12205589; my $element_name = "foo"; $element_stats_hash{$element_name}{'raw_data'}{$time} = $val; # <-- print Dumper $element_stats_hash{$element_name}; __END__ $VAR1 = { 'raw_data' => { '1229382894' => '0.12205589' } };

    Update: That said, there's nothing wrong with using an intermediate variable, in particular if you should end up needing it multiple times:

    # init raw data hash somewhere $element_stats_hash{$element_name}{'raw_data'} = {}; # then later my $raw_data = $element_stats_hash{$element_name}{'raw_data'}; $raw_data->{$time} = $val; $raw_data->{$another_time} = $another_val; #...

    But note the difference with respect to autovivication... which is why we need to init an (empty) 'raw data' hash in this case, before we can take a ref to it.

    Also, please use the arrow syntax for dereferencing — it's much more readable (and less ambiguous) than $$raw_data{$time} when the expressions get more complex...

      Hi,

      Thanks. I believe I tried your suggestion carefully and it still does the same thing.

      ???

      Tony

        Could you post the exact code you tried?  Anything else would require mind reading on our part to figure out where the problem lies :)  — Self-contained snippets are always preferred...