Hi :-) I wrote this binary search function. I wrote it so that I could pass a comparison function as the last parameter. But I have to write "sub" and I noticed that the built in sort function doesn't need it. So I have to write:
sub { shift <=> shift} instead of: {$a <=> b}. This might be a silly question, but I'd like to know if I can modify the binary search function to receive a function similar to the one that "sort" receives. #/usr/bin/perl -w use strict; # By sefault it works on strings sub binary_search { my $arr = shift; my $value = shift; my $cmpf = shift || sub {shift cmp shift}; my $left = 0; my $right = @$arr - 1; while ($left <= $right) { my $mid = int($right + $left) >> 1; my $c = &$cmpf($arr->[$mid], $value); if ($c == 0) { return 1 } if ($c > 0) { $right = $mid - 1 } else { $left = $mid + 1 } } return 0 }; my @arr = qw(1 2 3 4 5 6 7 8); for (0 .. 9) { print binary_search([EMAIL PROTECTED], $_) . "\n" } Regards, Nelson.- -- http://arhuaco.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/