[Bioperl-l] weird behavior of arrary reference arguments for a subroutine

Andreas Kahari ak at ebi.ac.uk
Thu May 19 12:04:05 EDT 2005


Dear Guojun Yang,

You program produces the following output:

A:3 B:4
A:5 B:6
A:7 B:8

(you array indices starts on 1, not on 0, so you miss the fist
array entry).

I'm slightly surprised that "${$_[0]}[$X]" generates anything
useful.  In fact, "${$_[0]}" is an error in itself since
"$_[0]" is not a scalar reference.  I would have written it as
"$_[0]->[$X]" (when thinking about it, I would probably have
extracted the references from @_ in the start of the subroutine
into something with more useful variable names).


Andreas

On Thu, May 19, 2005 at 11:27:11AM -0400, Guojun Yang wrote:
> Dear All,
> I have a subroutine that uses several array references as arguments (the arrays are produced earlier by another subroutine, and the array contents are correct when I print them out). But when I use the references as arguments for subroutine, it seems they are in wrong order. See sub below:
> 
> @A=(1, 3, 5, 7);
> @B=(2, 4, 6, 8);
> @C=(11,13, 15, 17);
> @D=(12, 14, 16, 18);
> 
> sub test {
> # example use: test(\@A, \@B, \@C, \@D)
> 
>       my $X=1;
>       while (${$_[0]}[$X]) {
>    print "A:${$_[0]}[$X] B:${$_[1]}[$X]\n";
> 
>         my $Y=1;
>         while (${$_[2]}[$Y]) {
>            .....
>         $Y+=1;
>         }
>       $X+=1;
>       }
> 
> I expect the result would print:
> A:1 B:2
> A:3 B:4
> A:5 B:6
> A:7 B:8
> 
> But the real print out is something like:
> ...(these may be correct ones).
> A:1 B:14
> A:1 B:16
> A:1 B:18
> ...
> A:15 B:6
> A:15 B:8
> ...
> 
> Does anybody have a clue on the weird thing?
> Thank you!
> Guojun
> 
> Department of Plant Biology
> University of Georgia
> 
> 
> _______________________________________________
> Bioperl-l mailing list
> Bioperl-l at portal.open-bio.org
> http://portal.open-bio.org/mailman/listinfo/bioperl-l
> 

-- 
Andreas Kähäri

EMBL-EBI/ensembl
www.ensembl.org

1024D/C2E163CB F4C4 A41A 665B 448A 3FA9  6AEA 12E3 39DA C2E1 63CB


More information about the Bioperl-l mailing list