CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
strict ref error for accessing the first element of an array

 



DanCtyDesperado
New User

Sep 23, 2007, 10:03 AM

Post #1 of 6 (695 views)
strict ref error for accessing the first element of an array Can't Post

Hello, I'm having a somewhat confusing problem with some of my Perl code. The code I'm using is as follows.

Code
sub incrementBreaks 
{
my $breaks = shift;
my @chroms = @_;

my %chromNames;
$chromNames{"ref|NC_001133|"} = 0;
$chromNames{"ref|NC_001134|"} = 1;
$chromNames{"ref|NC_001135|"} = 2;
$chromNames{"ref|NC_001136|"} = 3;
$chromNames{"ref|NC_001137|"} = 4;
$chromNames{"ref|NC_001138|"} = 5;
$chromNames{"ref|NC_001139|"} = 6;
$chromNames{"ref|NC_001140|"} = 7;
$chromNames{"ref|NC_001141|"} = 8;
$chromNames{"ref|NC_001142|"} = 9;
$chromNames{"ref|NC_001143|"} = 10;
$chromNames{"ref|NC_001144|"} = 11;
$chromNames{"ref|NC_001145|"} = 12;
$chromNames{"ref|NC_001146|"} = 13;
$chromNames{"ref|NC_001147|"} = 14;
$chromNames{"ref|NC_001148|"} = 15;
$chromNames{"ref|NC_001224|"} = 16;

foreach my $hash ( @{$breaks} )
{
if (defined $chroms[ $chromNames{ $hash->{"target"} } ])
{
if (defined $chroms[ $chromNames{ $hash->{"target"} } ]->[$hash->{"value"}])
{
$chroms[ $chromNames{ $hash->{"target"} } ]->[$hash->{"value"}]++;
}
else
{
$chroms[ $chromNames{ $hash->{"target"} } ]->[$hash->{"value"}] = 1;
}
}
else
{
my @chrom;
$chrom[$hash->{"value"}] = 1;
$chroms[ $chromNames{ $hash->{"target"} } ] = \@chrom;
}
}
}


The problem occurs with the code:
$chroms[ $chromNames{ $hash->{"target"} } ]->[$hash->{"value"}]

I keep getting the error:
Can't use string ("") as an ARRAY ref while "strict refs" in use

I've done some debugging and found that the problem comes when the value $chromNames{ $hash->{"target"} } = 0. So for instance, this does not work:
$chroms[ 0 ]->[$hash->{"value"}]
but this does:
$chroms[ 1 ]->[$hash->{"value"}]

Also there is no problem if I don't access the array being referenced. So for example, this causes no errors:
$chroms[ 0 ]

Thank you so much for trying to help me.


KevinR
Veteran


Sep 23, 2007, 10:41 AM

Post #2 of 6 (694 views)
Re: [DanCtyDesperado] strict ref error for accessing the first element of an array [In reply to] Can't Post

try putting quotes around the zero and see if that helps:

$chromNames{"ref|NC_001133|"} = '0';

in perl, 0 (zero) is a false value. Adding the quotes will maybe stringify the zero and hopefully prevent the problem you are having.
-------------------------------------------------


DanCtyDesperado
New User

Sep 23, 2007, 12:58 PM

Post #3 of 6 (691 views)
Re: [KevinR] strict ref error for accessing the first element of an array [In reply to] Can't Post

No such luck, I tried placing the single quotes around the 0 and I still got the same error. Any other ideas?


DanCtyDesperado
New User

Sep 23, 2007, 1:29 PM

Post #4 of 6 (690 views)
Re: [DanCtyDesperado] strict ref error for accessing the first element of an array [In reply to] Can't Post

Well, I still don't know what the problem is, but I found a work around. I think the array was just getting passed weird because when I moved the content of the subroutine into the subroutine that calls it, all was well again. :sighs: Silly computers.


rgkrishnas
Novice

Sep 23, 2007, 11:56 PM

Post #5 of 6 (684 views)
Re: [DanCtyDesperado] strict ref error for accessing the first element of an array [In reply to] Can't Post

The array you passing to the subroutine, should not be single dimentional array, i.e. not like the below line

incrementBreaks(\@breaks, "");

Note that the second parameter you are using as multi dimentional array, but your passing agrument is single dimentional array;

The below example will clear your doubts

use strict;
my @array;
$array[0]->[0] = 1;
$array[1]->[0] = 2;

print "$array[0][0]\n";
print "$array[1][0]\n";

Above code will works fine, but

use strict;
my @array = ("");
$array[0]->[0] = 1;
$array[1]->[0] = 2;

print "$array[0][0]\n";
print "$array[1][0]\n";

Error: Can't use string ("") as an ARRAY ref while "strict refs"

because the first index value you have declared as single dimentional array, so the error throughs you.

Check your parameter and passing the types of variable accordingly

i.e. it should be like this

incrementBreaks(\@breaks, []);

or

incrementBreaks(\@breaks);

By
Gopal


(This post was edited by rgkrishnas on Sep 24, 2007, 12:11 AM)


KevinR
Veteran


Sep 24, 2007, 10:24 AM

Post #6 of 6 (676 views)
Re: [rgkrishnas] strict ref error for accessing the first element of an array [In reply to] Can't Post

That is an excellent and very subtle point.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives