sub printarray {
   my ($array) = @_; # since you are passing a reference, which is a scalar
   print join("\n", @$array) . "\n";  # @$array is convenient shorthand for
@{$array}
}

The @{$array} syntax is useful when you have more complex structures (i.e. a
reference to a hash of arrays) where you have to do multiple dereferences,
for example:

push(@{$groups->{$groupname}}, $user);

The O'Reilly 2nd and 3rd edition of the camel book does a very good job of
explaining how to dereference in a large variety of cases.

-Peter

> -----Original Message-----
> From: Philip Morley [mailto:[EMAIL PROTECTED]] 
> Sent: Tuesday, May 14, 2002 8:41 AM
> To: [EMAIL PROTECTED]
> Subject: Passing arrays into subroutines by reference
> 
> 
> The O'Reilly books don't really cover this subject, and I'm 
> having difficulty figuring out how to do so.  The script:
> 
> sub printarray {
> 
>   my @array = @_;
>   my $i;
> 
>   foreach $i (@array) {
>     print "$i\n";
>   }
> 
> }
> 
> my @flintstones = qw(fred, wilma, pebbles); printarray(\@flintstones);
> 
> prints ARRAY(0xb92e98), which I guess is the memory address 
> of @flintstones.  But I'd like to print the contents of the 
> array.  Probably a straightforward thing to do, but I have 
> RTFM and still don't know how.  Any suggestions?
> 
> Thanks,
> 
> Phil Morley
> 
> 
> 
> _______________________________________________
> ActivePerl mailing list
> [EMAIL PROTECTED]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
> 
_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to