jaichaud has asked for the wisdom of the Perl Monks concerning the following question:

I need to pass three arrays to a subroutine (the arrays may change based on certain conditions). What I have but doesn't work:
sub myCurve(@@@$$); $my_exp = myCurve(\@my_arx,\@my_ary,\@my_arz,$my_v1,$my_v2); sub myCurveX(@@@$$) { my $my_y = pop; my $my_x = pop; my ($my_xar,$my_yar,$my_zar) = @_; my @local_x = @$my_xar; my @local_y = @$my_yar; my @local_z = @$my_zar; # do some stuff return $my_calc; }
My scalars get passed and I can manipulate them, but the arrays seem to be void. I have looked at lots of examples and tried a number of ideas, but nothing seems to work. I really appreciate in advance any advice/help.

Replies are listed 'Best First'.
Re: Passing three arrays to a subroutine
by zentara (Cardinal) on Nov 09, 2010 at 16:50 UTC
    Possibly your prototyping is wrong, since you are passing in arrayref's, not actual arrays.
    sub myCurveX($$$$$)

    But why even use prototypes in the sub?

    #$my_exp = myCurveX(\@my_arx,\@my_ary,\@my_arz,$my_v1,$my_v2); #sub myCurveX(){ # thanks to [jwkrahn] (below) for pointing out my co +de sloppiness sub myCurveX{ #correct.... no () my ($arrayref1, $arrayref2,$arrayref3, $v1, $v2) = @_; $arrayref1->[0] = 1; #sets first element of @my_arx to 1

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      But why even use prototypes in the sub?
      $my_exp = myCurveX(\@my_arx,\@my_ary,\@my_arz,$my_v1,$my_v2); sub myCurveX(){ my ($arrayref1, $arrayref2,$arrayref3, $v1, $v2) = @_;

      You say "why even use prototypes" and then you use a prototype that says accept zero arguments, but the next line says to accept five arguments?

        Well you have to read in values into the sub somehow, unless you use global variables. How else would he do it?

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
Re: Passing three arrays to a subroutine
by chromatic (Archbishop) on Nov 09, 2010 at 16:54 UTC

    Your prototype is definitely wrong, because any unescaped @ in a prototype eats up all of the remaining arguments. With that said, don't use prototypes; they don't do what you expect them to do, especially here.

    We can't debug the rest of the problem because we don't see the rest of the program.

      I am sending three arrays like this:
      @my_arx=(1.0,2.0) @my_ary=(3.0,4.0,5.0) @my_arz=(10.0,20.0,30.0,40.0,50.0,60.0) # z = f(x,y)
      So in my subroutine I don't want to change the array values, but I need to use them in some calculations. The arrays passed to the subroutine are chosen based on some conditions. The calculations work in the subroutine - it is just getting the array values to it. I initially put the arrays in the subroutine until I realized that there would be four different sets of arrays to possibly come into play.
        Thanks for the quick responses. I found the problem - I entered @$my_ref_x in one place and $my_x_ref in the subroutine.

        Sometimes it's hard to see the trees through the forest.

        Thanks again.
Re: Passing three arrays to a subroutine
by tod222 (Pilgrim) on Nov 09, 2010 at 17:06 UTC

    Try something more like this:

    #!/usr/bin/perl use strict; use warnings; my @arx; my @ary; my @arz; my $v1; my $v2; my $exp = myCurve( \@arx, \@ary, \@arz, $v1, $v2 ); sub myCurve { my ( $xar, $yar, $zar, $x, $y ) = @_; my $calc; my @local_x = @$xar; my @local_y = @$yar; my @local_z = @$zar; # do some stuff return $calc; }

    Also, if the arrays are large you might want to drop the my @local_x = @$xar lines and operate using the array references.

    Note that even though subroutines can be pre-declared with prototypes, this isn't widely used in Perl. See Gratuitous use of Perl Prototypes.

Re: Passing three arrays to a subroutine
by locked_user sundialsvc4 (Abbot) on Nov 09, 2010 at 18:50 UTC

    A strategy that I like to use in such cases is to pass in one hashref that (must) contain the arrayrefs (or whatever) that I expect.

    In my typical style, the subroutine aggressively checks its arguments.   If anything is not-quite-right, it uses Data::Dumper to dump the list, and then and either croaks, or confesses its sins. (Carp).

Re: Passing three arrays to a subroutine
by choroba (Cardinal) on Nov 09, 2010 at 16:53 UTC
    When I remove the X from the sub name and populate the input arrays, the local arrays get populated as well.