in reply to Prototype like sort()?
This sort subroutine can be called with a code block just as Perl's internal sort. For example:sub comb_sort (&\@) { my $code_ref = shift; my $v_ref = shift; my $max = scalar (@$v_ref); my $gap = $max; while (1) { my $swapped = 0; $gap = int ($gap / 1.3); $gap = 1 if $gap < 1; my $lmax = $max - $gap - 1; foreach my $i (0..$lmax) { local ($a, $b) = ($$v_ref[$i], $$v_ref[$i+$gap]); ($$v_ref[$i], $$v_ref[$i+$gap], $swapped) = ($$v_ref[$i+$g +ap], $$v_ref[$i], 1) if $code_ref->($a, $b) > 0; } last if $gap == 1 and $swapped == 0; } }
I'm not sure that's what you're after, but you can see above a calling syntax with a simple code block (no need to build an actual subroutine).#!/usr/bin/perl use strict; use warnings; my @v; my $max = 500; $v[$_] = int rand(20000) foreach (0..$max); comb_sort {$a<=>$b} @v; print "@v";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Prototype like sort()?
by perlancar (Hermit) on Jan 29, 2018 at 13:47 UTC | |
by Laurent_R (Canon) on Jan 29, 2018 at 18:45 UTC |