in reply to getting arrays into subroutines

As others have aluded to, you cannot pass an array to a subroutine, only a list of scalar values. When you pass multiple arrays as mysub(@array1, @array2, @array3), the three arrays become concatenated into one big list. The solution is to use references to the arrays. Be careful that you copy the dereferenced array unless you want to change the values of the original array. Example:

my @a1 = qw/a b c d e f g/; my @a2 = qw/h i j k l m n/; sub mysub { my ($r1, $r2) = @_; # make a copy of the first array my @a1_copy = @{ $r1 }; $a1_copy[2] = 'blah'; # only affects this copy # tweak the original @a2 $r2->[2] = 'blah; # affects @a2 } mysub(\@a1, \@a2);

Replies are listed 'Best First'.
Re: Re: getting arrays into subroutines
by frankus (Priest) on Aug 13, 2002 at 08:36 UTC