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

Hi monks!

I am having a problem with my OOP data structure! Basically, I have an array that holds arrays of data. This data is just 1's and 0's.
Like This:
-----------
array cell 0 = (1,0,0,0,1,1,0)
array cell 1 = (0,0,0,1,1,1,1,0,0,1,1,1)
array cell 2 = (1,1,0,1,1,1,1,0,0,1,1,1)
etc...

My constructor looks something like this:

sub new { my ($class) = @_; my $self = { _length => "", _score => [ [],[] ], _coordinates => [], }; bless {$self,$class}; } # END "new" method

What I need to do is go through each data array in my @{$self->_score}[] array and output a comma-delimited list of coordinates whenever a string of 1's is encountered...

Like this:
if @{$self->_score}index = (1,1,1,0,0,1,1,0)
then i want
@($self->_coordinates}index = "0-2,5-6"

Easy Right?

But why can't I just do this:

sub findcoordinates { my ($self) = @_; my $first; my $last; for (my $i=2;$i <= 5;$i++) { my $base = 0; while($base <= (($self->{_length})-$i)) { if (@{$self->{_score}[$i][$base]} = "1") { $first = $base; while (@{$self->{_score}[$i][$base]} ne "0") { $last = $base; $base++; } @{$self->{_coordinates}[$base] = @{$self->{_coordin +ates}[$base] + "$first-$last"; } else { $base++; } } } } #END findcoordinates method

What this all means is that IF the Data cell = '1', continue to move down the array until the data cell isn't equal to '1' anymore. Then print out the first cell # and the last one parsed like this: "3-5" or something.
Problem is I always get a Can't use undefined value as an array reference error? Why do i get this error? Any suggestions as to a better way to accomplish my monk-ish task?

Replies are listed 'Best First'.
Re: Parsing and scoring an Array of Arrays
by chromatic (Archbishop) on Aug 09, 2001 at 00:56 UTC
    My guess is this code:
    while (@{$self->{_score}[$i][$base]} ne "0") { $last = $base; $base++; }
    How many items are in the array at $i? Your code never checks. It keeps incrementing $base. Eventually, it's going to run out of items, and Perl will happily attempt to oblige you by autovivifying a new array entry at the new position. It won't be an array reference, though, and there's your error.

    As a side note, be careful with your comparisons. You almost certainly don't want to say if ($x = 1) {...

      ObRandomTip: To avoid making the mistake:
      if ($x = 1) { # etc }
      get into the habit of trying to put the variables on the right, not the left. It looks strange, but:
      if (1 = $x) { # etc }
      is likely to give you a much more informative error message. (This habit will serve you in good stead in quite a few languages, not just Perl.)
      Chromatic, I think that you are right...
      Thanks,

      Mr. Don't!
Re: Parsing and scoring an Array of Arrays
by dragonchild (Archbishop) on Aug 09, 2001 at 00:49 UTC
    I'd reccomend putting a bunch of print statements or using the debugger. Find out where the actual bomb is happening. :)

    ------
    /me wants to be the brightest bulb in the chandelier!

    Vote paco for President!