Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl sort different from unix sort

Reply
Thread Tools

Perl sort different from unix sort

 
 
Jose Luis
Guest
Posts: n/a
 
      03-03-2011
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
 
Reply With Quote
 
 
 
 
Charlie Harvey
Guest
Posts: n/a
 
      03-03-2011
On 03/03/11 16:52, Jose Luis wrote:
> 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<---------------
 
Reply With Quote
 
 
 
 
Randal L. Schwartz
Guest
Posts: n/a
 
      03-03-2011
>>>>> "Jose" == Jose Luis <(E-Mail Removed)> writes:

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

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<(E-Mail Removed)> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion
 
Reply With Quote
 
Steve C
Guest
Posts: n/a
 
      03-03-2011
On 03/03/2011 11:52 AM, Jose Luis wrote:
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
perl print behavior different on window/unix jbilla2004@gmail.com Perl Misc 1 10-06-2006 06:12 PM
can Perl Sort do this, unix sort breaks on it (muliple spaces as demiliter) colin_lyse Perl Misc 1 02-03-2005 01:13 AM
my own perl "dos->unix"/"unix->dos" Robert Wallace Perl Misc 7 01-22-2004 10:59 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments