Going back to my attitude when I was a C programmer: It pays to know how your compiler thinks. (Needs adjustment for application to modern use of Perl, but the sentiment is the same.)
Adding/replacing these three lines into my original script above:
use Encode qw( encode_utf8 );
my $x = chr(1 << 63);
my @Unsorted = ( 'Dog', 'Cat', 'Bird', undef, $x, 'Elephant', undef, '
+Lizard' );
Yields:
S:\Steve\Dev\PerlMonks\P-2017-06-12@0734-sort-undef>perl .\sort011.pl
----------------------------------------------------------------------
+---------
Original:
----------------------------------------------------------------------
+---------
Dog
Cat
Bird
(undef)
Wide character in print at .\sort011.pl line 58.
ÇêÇÇÇÇÇÇÇÇÇÇ
Elephant
(undef)
Lizard
----------------------------------------------------------------------
+---------
----------------------------------------------------------------------
+---------
Custom Sort:
----------------------------------------------------------------------
+---------
Bird
Cat
Dog
Elephant
Lizard
(undef)
(undef)
Wide character in print at .\sort011.pl line 58.
ÇêÇÇÇÇÇÇÇÇÇÇ
----------------------------------------------------------------------
+---------
S:\Steve\Dev\PerlMonks\P-2017-06-12@0734-sort-undef>
A string of chr(255)bytes longer than the longest item in the original array still fails to sort to the bottom; knowing that Unicode characters are stored differently than old-fashioned ASCII strings empowers the Perl programmer to make a better choice.
Thank you for the information!
I'd upvote the post, but there isn't any point, as it's Anonymous Monk.
-
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.
|