#1
  1. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2000
    Location
    Hong Kong
    Posts
    71
    Rep Power
    15

    Angry


    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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    300
    Rep Power
    15
    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.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Kuwait
    Posts
    47
    Rep Power
    15
    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

IMN logo majestic logo threadwatch logo seochat tools logo