Andrew Gaffney wrote:
> 
> I'm trying to write a subroutine that takes two scalars and two arrays as 
> parameters. I've
> read that if you try to do this in a function, both arrays will get combined within 
> '@_'.

All the values, including the two scalars, will be combined in a single
list and be accessible through the @_ array.

> Instead, I want the subroutine to take two scalars and two array references. I will 
> not be
> doing any modification of the array data in the subroutine, so it will be more 
> effecient
> this way, anyway. I vaguely remember reading in Learning Perl (or maybe Programming 
> Perl)
> that the subroutine can be defined something like:
> 
> sub my_subroutine([EMAIL PROTECTED]@) {
> 
> }

Yes.  The perlsub.pod document which should be on your hard drive
contains a more complete explanation of how subroutines work in Perl.

> My syntax may be off. Someone please correct me if so. (Do I really need to say 
> this?)
> Now, how do I get those values in the subroutine?
> 
> sub my_subroutine([EMAIL PROTECTED]@) {
>    my ($scalar1, $scalar2, $arrayref1, $arrayref2) = @_;
> }

Yes.

> Another thing, how do you access an array through a reference?

To access a complete array:

@$arrayref1

> I know you access a hash
> through a reference by doing '$hashref->{hashkey}' instead of just 
> '$hashref{hashkey}',
> but I've never done it with arrays (never needed to).

To access an array element:

$arrayref1->[ 4 ]

> One more thing (I promise). Do I
> need to do anything special to pass arrays as references to the function, like this:
> 
> my_subroutine $scalar1, $scalar2, [EMAIL PROTECTED], [EMAIL PROTECTED];
> 
> or can I pass them without the '\'?

Without the '\'.  perl uses the prototype ([EMAIL PROTECTED]@) to convert the arrays
to references.


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to