Stephen Spalding wrote:
> Hello all,
>
> I have a question about perl. I'm trying to pass an
> array into a subroutine, but I don't know what the
> proper way to receive it in the subroutine is. Below
> is an example of what I'm trying to do. The ???
> represents what I do not know what to put in.
>
> @sample_array = ('hi', 'there', 'steve');
use strict;
my @sample_array = qw(hi there steve);
> &PRINT_CONTENTS("@sample_array");
Don't use the quotes. Those interpolate the array into a single long string.
Also, lose the ampersand:
PRINT_CONTENTS(@sample_array);
> exit 0;
>
> sub PRINT_CONTENTS
> {
> @local_array = ???
Perl passes all arguments in a single array, @_
my @local_array = @_;
>
> foreach $string (@local_array)
> {
> print "string = $string\n";
> }
> }
You could also just use @_ directly and simplify the sub to
sub PRINT_CONTENTS {
print "string = $_\n" for @_;
}
It's also common to shift elements one at a time off of @_:
sub PRINT_CONTENTS {
print "string = ", shift, "\n" while @_;
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]