chrism01 has asked for the wisdom of the Perl Monks concerning the following question:
I've been trying to learn a bit more about sorting, but was having problems with 'A brief tutorial on Perl's native sorting facilities' A brief tutorial on Perl's native sorting facilities. by BrowserUk.
The desc and examples looked just fine and I seemed to understand it, but couldn't reproduce it...
After some experimentation and inspiration I discovered the following:
From perldocs:
produced this:#!/usr/bin/perl -w use strict; use locale; print +(sort grep /\w/, map { chr } 0..255), "\n"; no locale; print +(sort grep /\w/, map { chr } 0..255), "\n";
My test prog
Outputs:#!/usr/bin/perl -w use strict; my ( @arr1 ); # GRT # Sort by num, then letter use locale; print "locale\n"; @arr1 = map{ unpack 'x[NA1]A*', $_ } sort map{ pack 'NA1 A*', substr( $_, 1 ), substr( $_, 0, 1 ), $_ } qw[ A473 B437 B659 C659 C123 D123 D222 E222 E001 A001 ]; print join("\n", @arr1)."\n\n"; no locale; print "no locale\n"; @arr1 = map{ unpack 'x[NA1]A*', $_ } sort map{ pack 'NA1 A*', substr( $_, 1 ), substr( $_, 0, 1 ), $_ } qw[ A473 B437 B659 C659 C123 D123 D222 E222 E001 A001 ]; print join("\n", @arr1)."\n";
locale A001 A473 B437 B659 C123 C659 D123 D222 E001 E222 no locale A001 E001 C123 D123 D222 E222 B437 A473 B659 C659
It seems to me that regardless of locale/no locale, it should sort nums before letters, as per BrowserUK's tutorial.
FYI:
perl v5.8.8, Linux openSUSE 10.2 (i586) 2.6.18.2-34-bigsmp
Also: env|grep -i lc has no output, set|grep -i lc gives:
MAILCHECK=60 MODLIST=($(LC_ALL=C $YAST -l| grep '^[a-z]' | grep -v "Availab +le")); done <<(LC_ALL=C $YAST $mod $prev help 2>&1); done <<(LC_ALL=C $YAST $mod help 2>&1); test_lc () for lc in LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY +LC_MESSAGES LC_PAPER LC_NAMELC_ADDRESS LC_TELEPHONE LC_MEASUREMENT LC +_IDENTIFICATION LC_ALL; eval val="\$$lc"; unset lc val; unset lc val;
Cheers
Chris
20071129 Janitored by Corion: Localized link
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Sorting (GRT) and locale issues
by ikegami (Patriarch) on Nov 15, 2007 at 04:46 UTC | |
Re: Sorting (GRT) and locale issues
by salva (Canon) on Nov 15, 2007 at 09:22 UTC | |
Re: Sorting (GRT) and locale issues
by chrism01 (Friar) on Nov 15, 2007 at 05:11 UTC | |
by ikegami (Patriarch) on Nov 15, 2007 at 05:38 UTC | |
Re: Sorting (GRT) and locale issues
by chrism01 (Friar) on Nov 15, 2007 at 06:14 UTC | |
by ikegami (Patriarch) on Nov 15, 2007 at 06:28 UTC |