Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: getting arrays into subroutines

by mfriedman (Monk)
on Aug 12, 2002 at 19:16 UTC ( #189591=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://189591]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2021-06-22 11:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (104 votes). Check out past polls.

    Notices?