Another slight variation on a custom sort sub:
c:\@Work\Perl\monks>perl -le
"use warnings;
use strict;
;;
use Test::More 'no_plan';
use Test::NoWarnings;
;;
my $OptOrd = 1;
;;
my $Test = [
[ 1, 'c' ],
[ 2, 'a' ],
[ 3, '' ],
[ 4, undef ],
[ 5, 'foo' ],
[ 6, '' ],
[ 7, 'cee' ],
[ 8, undef ],
[ 9, 'tee' ],
[ 10, 't' ],
];
;;
my $Expected = [
[ 3, '' ],
[ 6, '' ],
[ 2, 'a' ],
[ 1, 'c' ],
[ 7, 'cee' ],
[ 5, 'foo' ],
[ 10, 't' ],
[ 9, 'tee' ],
[ 4, undef ],
[ 8, undef ],
];
;;
my @got = sort by_deaccented_ascending_with_undef_highest @$Test;
;;
is_deeply \@got, $Expected, 'custom sort sub';
;;
done_testing;
;;
sub deaccent { return $_[0]; }
;;
sub by_deaccented_ascending_with_undef_highest {
my ($aa, $bb) = ($a->[$OptOrd], $b->[$OptOrd]);
;;
return defined $aa && defined $bb
? deaccent($aa) cmp deaccent($bb)
: defined $bb cmp defined $aa
;
}
"
ok 1 - custom sort sub
1..1
ok 2 - no warnings
1..2
defined returns
'' (empty string) for undefined,
1 for defined, and
'' will lexically
cmp below
1.
Give a man a fish: <%-{-{-{-<
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.