Gossamer Forum
Home : General : Perl Programming :

using undef to reset an array

Quote Reply
using undef to reset an array
hi,

i have an array that i want to reuse in a loop. i was resetting the array using @this = ''; until i realized that the $this[0] became ''

now, i'm using undef @this; i'm wondering if this is the way folks generally reuse arrays in loops. here's the base of the code. is this the most efficient, best way to do it? thanks!

Code:


my @this;
foreach my $file (@files) {
undef @this;
open (IN, "<$file") or die "$!";
while (<IN>) {
m|^\d{4}| && push (@this, $_);
}
close(IN) or die "$!";
&send_output(\@this);
}
Quote Reply
Re: [adrockjames] using undef to reset an array In reply to
undef will nuke the array as if it never existed. I normally just use:

my @array = ();

...so you can remove my @this; at the top and just add:

my @this = ();

...where you have:

undef @this;

Last edited by:

Paul: Apr 3, 2003, 12:37 AM
Quote Reply
Re: [Paul] using undef to reset an array In reply to
yes, of course....thanks paul.

what would be a situation where undef should be used?
Quote Reply
Re: [adrockjames] using undef to reset an array In reply to
See:

http://www.perldoc.com/.../pod/func/undef.html