Jeff 'japhy' Pinyan <[EMAIL PROTECTED]> wrote:
:
: On Apr 10, Richard Heintze said:
:
: > I'm passing a singularly dimensioned array to
: > a function. I need to clear the array in the
: > function so that when I return to the main
: > program, there are no entries in the array
: > variable I passed to the function.
:
: You have to pass the array by reference, then.
:
: my @array = (1 .. 4);
:
: print "(@array)\n"; # (1 2 3 4)
: my_func([EMAIL PROTECTED]);
: print "(@array)\n"; # ()
:
: sub my_func {
: my $array_ref = shift;
: @$array_ref = ();
: }
Jeff's example will do what you want, but I'm not
certain it is a Good Thing. I try to avoid modifying
function arguments inside a function. There are some
good reasons, like when the array is very large, but
in general I would advise avoiding this.
This seems clearer to me:
my @array = ( 1 .. 4 );
my_func([EMAIL PROTECTED]);
@array = ();
sub my_func {
my $array_ref = shift;
# do stuff
}
Actually, it makes me wonder why we are using
@array at all:
my_func( [ 1 .. 4 ] );
sub my_func {
my $array_ref = shift;
# do stuff
}
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328
Get real! This is a discussion group, not a helpdesk.
You post something, we discuss its implications. If
the discussion happens to answer a question you've
asked, that's incidental. - nobull in comp.lang.perl.misc
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>