[Bioperl-l] weird behavior of arrary reference arguments for a
subroutine
Paulo Almeida
palmeida at igc.gulbenkian.pt
Thu May 19 12:12:08 EDT 2005
Hi,
I don't fully understand your code and final goal (do you want to have just
output you mentioned, or add the C and D lists eventually? And in what
order?). This code, however, produces the output you wanted, in my computer
(I only removed the lines with $Y, but it worked just the same, and I also
changed $X to 0):
@A=(1, 3, 5, 7);
@B=(2, 4, 6, 8);
@C=(11,13, 15, 17);
@D=(12, 14, 16, 18);
sub test {
my $X=0;
while (${$_[0]}[$X]) {
print "A:${$_[0]}[$X] B:${$_[1]}[$X]\n";
$X+=1;
}
}
test(\@A, \@B, \@C, \@D);
-------
Output:
A:1 B:2
A:3 B:4
A:5 B:6
A:7 B:8
-Paulo
On Thursday 19 May 2005 16:27, 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
More information about the Bioperl-l
mailing list