469,270 Members | 1,164 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

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

Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. sub Peptides {
  2.     my ($self, @peptides) = @_;
  3.     my (@newpep, @temp);
  4.     # @peptides is considered a 3D Array with
  5.     # dimensions of i: 1, j: no. of peptides, k: 7 (data about peptides colleted)
  6.     #This will reformate the array correctly
  7.     if (defined @peptides) {
  8.         for my $i ( 0 .. $#peptides ) {
  9.             my $ref = $peptides[$i];
  10.  
  11.             for my $j ( 0 .. $#{$ref} ) {
  12.                 my $reef = $peptides[$i][$j];
  13.  
  14.                 for my $k (0 .. $#{$reef} ) {
  15.                     #add element from peptides to list @temp
  16.                     push @temp, $peptides[$i][$j][$k];
  17.                 }    #@temp should now be in the format (hitID, mass, delta, sequence, mod, score)
  18.  
  19.                 if ($#temp != 6) { shift @temp;}    #formatting issue with the first item in the array
  20.                 # add @temp to 2-d array @newpep
  21.                 push @newpep, [ @temp ];                                
  22.                 @temp = undef;    # clear temp for next iteration. 
  23.             }    
  24.         }
  25.  
  26.     $self->{_Peptides} = @newpep;
  27.     return $self->{_Peptides};
  28.     }#end if
  29.  
  30.     #no peptide data passed
  31.     return $self->{_Peptides};    
  32. }
is the object code in question, if that helps
Jun 26 '07 #1
7 22836
miller
1,089 Expert 1GB
Your code could be simplified a lot. But ignoring that, take a look at this line:

$self->{_Peptides} = @newpep;

- Miller
Jun 26 '07 #2
Well i changed it to
Expand|Select|Wrap|Line Numbers
  1. @ {$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.
Jun 26 '07 #3
miller
1,089 Expert 1GB
That would work. This would be better though:

Expand|Select|Wrap|Line Numbers
  1. $self->{_Peptides} = \@newpep;
  2.  
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.

Expand|Select|Wrap|Line Numbers
  1. return $self->{_Peptides} || [];
  2.  
- Miller
Jun 26 '07 #4
miller
1,089 Expert 1GB
PS,

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

Expand|Select|Wrap|Line Numbers
  1. sub Peptides {
  2.     my ($self, @peptides) = @_;
  3.  
  4.     # @peptides is considered a 3D Array with
  5.     # dimensions of i: 1, j: no. of peptides, k: 7 (data about peptides colleted)
  6.     # This will reformate the array correctly
  7.     if (@peptides) {
  8.         my @newpep = ();
  9.         foreach my $arrayI (@peptides) {
  10.             foreach my $arrayJ (@$arrayI) {
  11.                 foreach my $data (@$arrayJ) {
  12.                     # @temp should now be in the format (hitID, mass, delta, sequence, mod, score)
  13.                     my @temp = $#$data != 6 ? @{$data}[1..$#$data] : @$data; # formatting issue with the first item in the array
  14.                     push @newpep, \@temp;
  15.                 }
  16.             }
  17.         }
  18.  
  19.         $self->{_Peptides} = \@newpep;
  20.     }
  21.  
  22.     return $self->{_Peptides} || [];
  23. }
In fact, that center assignment to @newpep could be simplified down to the following single line:

Expand|Select|Wrap|Line Numbers
  1. my @newpep = map {[$#$_ != 6 ? @_[1..$#$_] : @$_]} map {@$_} map {@$_} @peptides;
  2.  
- Miller
Jun 26 '07 #5
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
Expand|Select|Wrap|Line Numbers
  1. @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.
Jun 26 '07 #6
miller
1,089 Expert 1GB
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
Jun 26 '07 #7
KevinADC
4,059 Expert 2GB
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.
Jun 26 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

24 posts views Thread by Mohammd M. Hussain | last post: by
11 posts views Thread by L. Chen | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.