in reply to Re: Re: declaration of variables
in thread declaration of variables
Remember, undef is not bad, you just have to know when it is a good choice:my @final; my @array = qw(foo bar baz); for (0..$#array) { my $temp; for my $j (0..$#array) { $temp .= $array[$j]; } push @final, $temp; }
Here, it is perfectly fine because we are undefining a scalar. But, what happens if we push another array instead?my $temp; for (0..$#array) { for my $j (0..$#array) { $temp .= $array[$j]; } push @final, $temp; undef $temp; }
This is a bad choice. Now @final contains three anonymous, empty array references. What happened? We copied the contents of @array, but after we pushed our temp array, we undefined it - hence, we 'erase' the data we just pushed. But, had we pushed a new anonymous array reference that contained our temp array, then it works:for my $i (0..$#array) { my @temp = @array; push @final, \@temp; undef @temp; }
for my $i (0..$#array) { my @temp = @array; push @final, [@temp]; undef @temp; }
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: 3Re: declaration of variables
by bradcathey (Prior) on Sep 02, 2003 at 15:47 UTC |