kilinrax has asked for the wisdom of the Perl Monks concerning the following question:
While investigating Benchmark.pm I decided to try benchmarking different sorting algorithms, and have run into trouble with my implementation of <dfn>Merge Sort</dfn>;
Presumably I'm doing something naive here to do with the scoping of @array, which, in slightly different circumstances, could cause errors rather than just warnings.
So, what does the warning mean, and how should I declare @array to avoid it?
The function works perfectly, but with warnings turned on, spits out the following:sub Merge(@) { my @array = @_; MergeSort (0, $#array); return @array; sub MergeSort($$) { my $first = shift; my $last = shift; if ($last>$first) { my $mid = int(($last+$first)/2); MergeSort($first, $mid); MergeSort($mid+1, $last); my @b; @b = ( @array[$first..$mid], @array[reverse($mid+1..$last)] ); my ($i, $j, $k) = (0, $last-$first, $first); for (; $k<=$last; $k++) { $array[$k] = ($b[$i]<$b[$j]) ? $b[$i++] : $b[$j--]; } } } }
Where [x] is the first line in MergeSort() which manipulates @array.Variable "@array" will not stay shared at ./benchmark-sort.pl line [x] +.
Presumably I'm doing something naive here to do with the scoping of @array, which, in slightly different circumstances, could cause errors rather than just warnings.
So, what does the warning mean, and how should I declare @array to avoid it?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Scoped Variables in a Recursive Function.
by japhy (Canon) on Sep 28, 2000 at 16:46 UTC | |
by Adam (Vicar) on Sep 28, 2000 at 19:46 UTC | |
by japhy (Canon) on Sep 28, 2000 at 21:12 UTC | |
by Adam (Vicar) on Sep 28, 2000 at 23:59 UTC | |
Re (tilly) 1: Scoped Variables in a Recursive Function.
by tilly (Archbishop) on Sep 28, 2000 at 17:04 UTC | |
Re: Scoped Variables in a Recursive Function.
by extremely (Priest) on Sep 29, 2000 at 04:48 UTC |
Back to
Seekers of Perl Wisdom