Hi,

I am stuck on a basic package/OOP situation. This is my first time trying to write an OOP package. For this first attempt, I just want a package that performs linear regression.

I have things working up to the place of my inability to resolve a hash data structure. I attempt to do this inside various package functions.

Here is some of the package code:

package regression; use strict; sub new { my ($class) = @_; bless { 'raw_data' => $_[1], 'mean_x' => 0, 'mean_y' => 0, 'slope' => 0, 'y_intercept' => 0 }, $class; } sub get_mean_x { $raw_dataPtr = $_[0]{'raw_data'}; my $counter = 1; my $x_total = 0; print "raw_datePtr: $raw_datePtr\n"; foreach $x (keys %$raw_dataPtr) { ++$counter; $x_total += $x; } my $mean_x = ($x_total/$counter); $self->{'mean_x'} = $mean_x; return ($self->{'mean_x'}); }


The part of the code that is killing me is the following line:

$raw_dataPtr = $_[0]{‘raw_data’}

The following are other syntax attempts:

$raw_dataPtr = $$_[0]{‘raw_data’}

$raw_dataPtr = $self->{‘raw_data’}



The only thing I know is that on this one point, I don’t know what the hell I am doing. I need to resolve the pointer to the hash so that my little package can go about calculating the linear regression related stuff.

Oh, here is the code that is referencing the package:

#!/usr/bin/perl -w use strict; require ('/opt/eHealth/web/aview/perl/site/lib/regression.pm'); open (TEST,">testing.txt"); my ($mean_x,$mean_y,$slope,$y_intercept); my %raw_data = ( 1 => 100, 2 => 160, 2.5 => 182, 3 => 225 ); my $regr = regression->new(\%raw_data); $mean_x = $regr->get_mean_x; #$mean_y = $regr->get_mean_y; #$slope = $regr->get_slope; #$y_intercept = $regr->get_slope; print TEST "Mean_x: $mean_x\tMean_y: $mean_y\tSlope: $slope\t: y inter +cept: $y_intercept\n"; close (TEST);


Can someone show me how to resolve the hash of x,y data points that the package needs to work with? By the way, I have verified that my test code “sees” the package.

Thanks in advance,

Tony

In reply to basic OOP question by o2bwise

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.