Hi,
I am trying to benchmark these two sorting algorithms. However running the script below:
$ perl thiscode.pl
gives: Can't use an undefined value as an ARRAY reference at radix.pl line 23.
However the code execute smoothly with the normal usage. Only when I use 'cmpthese' the problem occur.
Any idea?
Thanks so much beforehand. -- Edward WIJAYA Singapore
__BEGIN__ #!/usr/bin/perl -w use strict; use Benchmark 'cmpthese';
my @array = qw(flow loop pool Wolf root sort tour);
#The script runs ok if I remove this 'cmpthese' snippet
cmpthese (-5, {rad => "radix_sort([EMAIL PROTECTED])",
nsort => "basic_sort([EMAIL PROTECTED])"
}
);my @thear = basic_sort([EMAIL PROTECTED]); print "@thear\n";
sub basic_sort {
my $array = shift;
my @ar2;
@ar2 = sort @{$array}; #This is line 23, but seems that this is not the problem
return @ar2;
}
sub radix_sort { my $array = shift;
my $from = $array;
my $to; # All lengths expected equal.
for ( my $i = length ($array->[ 0 ]) - 1; $i >= 0; $i-- ) {
# A new sorting bin.
$to = [ ];
foreach my $card ( @$from ) {
# Stability is essential, so we use push().
push @{ $to->[ ord( substr $card, $i ) ] }, $card;
}# Concatenate the bins.
$from = [ map { @{ $_ || [ ] } } @$to ];
}# Now copy the elements back into the original array.
@$array = @$from; }
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>
