|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
I want to pass two array into the subroutine, but it doesn't work.
the correct result: ------------------------- first: 1,2,3 second: a,b,c pre: 1,2,3 pos: a,b,c How can I correct it? Thanks!!! the result after running: ------------------------- first: 1,2,3 second: a,b,c pre: 1,2,3 pos: 1,2,3 the perl script here: --------------------- #!/usr/bin/perl $first[0]=1; $first[1]=2; $first[2]=3; $second[0]="a"; $second[1]="b"; $second[2]="c"; print "first: $first[0],$first[1],$first[2]\n"; print "second: $second[0],$second[1],$second[2]\n"; &testing(@first,@second); sub testing { my @pre=@_; my @pos=@_; print "pre: $pre[0],$pre[1],$pre[2]\n"; print "pos: $pos[0],$pos[1],$pos[2]\n"; } ---------------------
__________________
RayRay p(^_^)q |
|
#2
|
|||
|
|||
|
I think the answer to your question lies in the use of references. Try reading the tutorials here at DevShed (in the Perl section) or try rading the perlref manpage for more gritty details.
|
|
#3
|
|||
|
|||
|
Use references to send arrays to the program. What your program does is send @first and @second, and then stores both in @pre. Insead send the addresses of the arrays to the subroutine, and then dereference them.
#!/usr/bin/perl $first[0]=1; $first[1]=2; $first[2]=3; $second[0]="a"; $second[1]="b"; $second[2]="c"; print "first: $first[0],$first[1],$first[2]\n"; print "second: $second[0],$second[1],$second[2]\n"; &testing(\@first,\@second); sub testing { my $x=$_[0]; my $y=$_[1]; @pre = @$x; @pos = @$y; print "pre: $pre[0],$pre[1],$pre[2]\n"; print "pos: $pos[0],$pos[1],$pos[2]\n"; }
__________________
Thank you |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Subroutines problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|