Can't use string ("0") as an ARRAY ref while "strict refs" in use

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lukertin
    New Member
    • Jun 2007
    • 4

    Can't use string ("0") as an ARRAY ref while "strict refs" in use

    I have a 2-D array stored as an object, when i go to call up the array using

    Code:
    my $testvar = @ {$self->Peptides};
    it gives me the error

    Can't use string ("0") as an ARRAY ref while "strict refs" in use.

    Currently I am trying to print the objects (the code is in the package code).

    When I try to do the same thing in my main code, it also gives the same error.

    I'm not exactly sure what is wrong at this point, any thoughts are welcome.

    Code:
    sub Peptides {
    	my ($self, @peptides) = @_;
    	my (@newpep, @temp);
    	# @peptides is considered a 3D Array with
    	# dimensions of i: 1, j: no. of peptides, k: 7 (data about peptides colleted)
    	#This will reformate the array correctly
    	if (defined @peptides) {
    		for my $i ( 0 .. $#peptides ) {
    			my $ref = $peptides[$i];
    			
    			for my $j ( 0 .. $#{$ref} ) {
    				my $reef = $peptides[$i][$j];
    				
    				for my $k (0 .. $#{$reef} ) {
    					#add element from peptides to list @temp
    					push @temp, $peptides[$i][$j][$k];
    				}	#@temp should now be in the format (hitID, mass, delta, sequence, mod, score)
    			
    				if ($#temp != 6) { shift @temp;}	#formatting issue with the first item in the array
    				# add @temp to 2-d array @newpep
    				push @newpep, [ @temp ];								
    				@temp = undef;	# clear temp for next iteration. 
    			}	
    		}
    	
    	$self->{_Peptides} = @newpep;
    	return $self->{_Peptides};
    	}#end if
    	
    	#no peptide data passed
    	return $self->{_Peptides};	
    }
    is the object code in question, if that helps
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Your code could be simplified a lot. But ignoring that, take a look at this line:

    $self->{_Peptides} = @newpep;

    - Miller

    Comment

    • lukertin
      New Member
      • Jun 2007
      • 4

      #3
      Well i changed it to
      Code:
      @ {$self->{_Peptides}} = @newpep;
      and fixed that issue everywhere else it pops up , it still gives the same error. I assume that's what you were referring to.

      Comment

      • miller
        Recognized Expert Top Contributor
        • Oct 2006
        • 1086

        #4
        That would work. This would be better though:

        [CODE=perl]
        $self->{_Peptides} = \@newpep;
        [/CODE]

        Additionally, change your final return statement to the following as it doesn't look like your initializing _Peptides before accessing it to assign to your variable.

        [CODE=perl]
        return $self->{_Peptides} || [];
        [/CODE]

        - Miller

        Comment

        • miller
          Recognized Expert Top Contributor
          • Oct 2006
          • 1086

          #5
          PS,

          Your code could be simplified by using foreach instead of relying on indexes:

          Code:
          sub Peptides {
              my ($self, @peptides) = @_;
              
              # @peptides is considered a 3D Array with
              # dimensions of i: 1, j: no. of peptides, k: 7 (data about peptides colleted)
              # This will reformate the array correctly
              if (@peptides) {
                  my @newpep = ();
                  foreach my $arrayI (@peptides) {
                      foreach my $arrayJ (@$arrayI) {
                          foreach my $data (@$arrayJ) {
                              # @temp should now be in the format (hitID, mass, delta, sequence, mod, score)
                              my @temp = $#$data != 6 ? @{$data}[1..$#$data] : @$data; # formatting issue with the first item in the array
                              push @newpep, \@temp;
                          }
                      }
                  }
                  
                  $self->{_Peptides} = \@newpep;
              }
              
              return $self->{_Peptides} || [];
          }
          In fact, that center assignment to @newpep could be simplified down to the following single line:

          Code:
          my @newpep = map {[$#$_ != 6 ? @_[1..$#$_] : @$_]} map {@$_} map {@$_} @peptides;
          - Miller

          Comment

          • lukertin
            New Member
            • Jun 2007
            • 4

            #6
            Miller,

            I did as you suggested, and the error no longer pops up.

            However, the array I assign the object to is empty.

            I'm guessing the way to fix it is to change something about how I assign the object to a new variable.

            Right now I have
            Code:
            @mypep = $self->Peptides;
            and since I never grasped how to reference in perl, I have no idea if that is right or not.

            btw, printing @mypep and $mypep[0] give the same result, (a reference to an array) but $mypep[0][x] gives nothing. I'm utterly baffled.

            Comment

            • miller
              Recognized Expert Top Contributor
              • Oct 2006
              • 1086

              #7
              Yes, it sounds like you need an education in complex datastructures for perl:

              perldoc perldsc Data Structures Cookbook

              It's hard suggesting where you code is wrong without being able to see and correct all the probably myriad of errors that you ahve. Just note that the error message that you got before was actually communicated something. For some reason the function you were calling returned the string "0". It's up to you to determine why that was the case. Maybe you weren't actually initializing the data? I don't know.

              Also, take advantage of the Data::Dumper module when working with complex structures. It's the easiest way to confirm that things are being stored in the way that you expect:

              perldoc Data::Dumper

              - Miller

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                You posted:

                I have a 2-D array stored as an object, when i go to call up the array using

                my $testvar = @ {$self->Peptides};
                $testvar would equal the length of the array on the right which is what assigning an array to a scalar does. That also explains the error message about using "0" as an array reference. Evidently the array was empty so length was zero.

                Comment

                Working...