Perl sort different from unix sort

J

Jose Luis

Hi,

How can I get the same sort for "foo.txt" in perl and unix sort?


<<snip>>

indra@bhsciences:/tmp$ cat foo.txt
1 2
10 1
indra@bhsciences:/tmp$ cat sort.pl

open my $fh, shift;

my @s1 = <$fh>;
print sort @s1;

indra@bhsciences:/tmp$ perl sort.pl foo.txt
1 2
10 1
indra@bhsciences:/tmp$ sort foo.txt
10 1
1 2
indra@bhsciences:/tmp$ uname -a
Linux bhsciences 2.6.32 #3 SMP Tue May 11 10:48:46 CEST 2010 x86_64 GNU/Linux


<<snip>>

Thanks in advance,
Jose Luis
 
C

Charlie Harvey

Hi,

How can I get the same sort for "foo.txt" in perl and unix sort?

Sort numerically with -n?

$ sort -n foo.txt
1 2
10 1
-----------------8<---------------
indra@bhsciences:/tmp$ perl sort.pl foo.txt
1 2
10 1
indra@bhsciences:/tmp$ sort foo.txt
10 1
1 2
-----------------8<---------------
 
R

Randal L. Schwartz

Jose> indra@bhsciences:/tmp$ perl sort.pl foo.txt
Jose> 1 2
Jose> 10 1
Jose> indra@bhsciences:/tmp$ sort foo.txt
Jose> 10 1
Jose> 1 2
Jose> indra@bhsciences:/tmp$ uname -a
Jose> Linux bhsciences 2.6.32 #3 SMP Tue May 11 10:48:46 CEST 2010
Jose> x86_64 GNU/Linux

Works fine on FreeBSD. Maybe Linux broke sort.

print "Just another Perl hacker,"; # the original
 
S

Steve C

Hi,

How can I get the same sort for "foo.txt" in perl and unix sort?


<<snip>>

indra@bhsciences:/tmp$ cat foo.txt
1 2
10 1
indra@bhsciences:/tmp$ cat sort.pl

open my $fh, shift;

my @s1 =<$fh>;
print sort @s1;

indra@bhsciences:/tmp$ perl sort.pl foo.txt
1 2
10 1
indra@bhsciences:/tmp$ sort foo.txt
10 1
1 2
indra@bhsciences:/tmp$ uname -a
Linux bhsciences 2.6.32 #3 SMP Tue May 11 10:48:46 CEST 2010 x86_64 GNU/Linux


<<snip>>

UNIX sort:
If no flags are specified, the sort command sorts entire lines of the input file
based upon the collation order of the current locale.

Perl sort:
If SUBNAME or BLOCK is omitted, "sort"s in standard string comparison order.
When "use locale" is in effect, "sort LIST" sorts LIST according to the current
collation locale. See perl-locale.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top