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

Is there a way to pass a whole array to a function. It looks like perl just take the elements one at a time.

=============== program =============
@data = ( 22, 33, 44 ); @newdata = ( 55, 66, 77 ); sub passingArray{ @tmp = $_[0]; @newtmp = $_[1]; print "Number of elements tmp: $#tmp\n"; print "tmp1 @tmp\n"; print "Number of elements newtmp: $#newtmp\n"; print "tmp1 @newtmp\n"; } passingArray( @data, @newdata );
================ output =======================
$ perl perl2.pl
Number of elements tmp: 0
tmp1 22
Number of elements newtmp: 0
tmp1 33

thanks in advance

Code tags - dvergin 2004-06-22

Replies are listed 'Best First'.
Re: passing whole array to a function
by borisz (Canon) on Jun 23, 2004 at 00:37 UTC
    if you want to pass array, use pass by reference. passingArray( \@data, \@newdata );
    sub passingArray { my ( $aref1, $aref2 ) = @_; print "Number of elements tmp: $#$aref1\n"; print "tmp1 @tmp\n"; print "Number of elements newtmp: $#$aref2\n"; print "tmp1 @$aref2\n"; }
    Boris
Re: passing whole array to a function
by ihb (Deacon) on Jun 23, 2004 at 01:00 UTC

    I have little to add, since your question has been answered already. I just want to give you some pointers where to read more about references, because sooner or later, and probably sooner, you will need to know how references works since there are pitfalls that you need to be aware of.

    These four documents all deal with references. I recommend you to read them in the same order I list them here.

    perlreftut perlref perllol perldsc

    Hope this helps,
    ihb

Re: passing whole array to a function
by Zaxo (Archbishop) on Jun 23, 2004 at 00:43 UTC

    Pass a reference to the array, either explicitly,

    sub array_fn { my @array = @{+shift}; # ... } array_fn(\@foo);
    or else implicitly with a prototype,
    sub array_fn (\@) { my $array = shift; # do things to @$array } array_fn(@foo);
    The prototype version is generally reserved for subs which will modify their argument array.

    After Compline,
    Zaxo

Re: passing whole array to a function
by data64 (Chaplain) on Jun 23, 2004 at 00:49 UTC

    Perl's passing of arrays and hashes is a little different from most other languages. So when you pass in an array @a to a subroutine, @_ will have (@#a +1 ) arguments. Essentially, it just unrolled the array for you.

    To achieve what you are trying to do, pass in references to the arrays. You can then use the references in your subroutine or dereference them into arrays if you prefer.

    use strict; use warnings; sub passingArray{ my $tmp = $_[0]; my $newtmp = $_[1]; print "Number of elements tmp: ". scalar( @$tmp ) . "\n"; print join( " ", @$tmp ) . "\n"; print "Number of elements newtmp: ". scalar( @$newtmp) . "\n"; print join( " ", @$newtmp) . "\n"; } #end of sub my @data = ( 22, 33, 44 ); my @newdata = ( 55, 66, 77 ); passingArray( \@data, \@newdata );

    Just a tongue-tied, twisted, earth-bound misfit. -- Pink Floyd