CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
how do i sort in a descending order using perl Sort::External Module

 



ravikim
New User

Nov 29, 2009, 4:39 AM

Post #1 of 2 (2867 views)
how do i sort in a descending order using perl Sort::External Module Can't Post

Hi ,

use Sort::External;
$output = '1.txt';
open(FILE, $output);
my $sortex = Sort::External->new( mem_threshold => 1024**2 * 1024 );
while (<FILE>)
{
$sortex->feed($_); # tried to change it to ~$_ but did not work !!!!!
}
use Fcntl;
$sortex->finish(
outfile => 'desc_Sorted_04323697_pbragg02@sprintpcs.com_.txt',
flags => ( O_CREAT | O_WRONLY ),
);
close(FILE);

with the above code the file is getting sorted in ascending order. how do i sort in descending order ?


1arryb
User

Dec 18, 2009, 9:41 AM

Post #2 of 2 (2562 views)
Re: [ravikim] how do i sort in a descending order using perl Sort::External Module [In reply to] Can't Post

Hi ravi,

Caveat: I haven't used Sort::External. Nevertheless, it seems most straightforward just to define the descending sort in a sortsub.

Code
use Sort::External; 
$output = '1.txt'; # You probably meant "input" here.
open(FILE, $output);
my $sortdesc = sub { $Sort::External::b <=> $Sort::External::a }; # Use cmp if your data are strings.
my $sortex = Sort::External->new( sortsub => $sortdesc, mem_threshold => 1024**2 * 1024 );
while (<FILE>) {
$sortex->feed($_); # tried to change it to ~$_ but did not work !!!!!
}
use Fcntl;
$sortex->finish (
outfile => 'desc_Sorted_04323697_pbragg02@sprintpcs.com_.txt',
flags => ( O_CREAT | O_WRONLY ),
);
close(FILE


The encode, sort, decode technique explicated in the Sort::External::Cookbook made my head hurt, but I don't think you're following it. Check out http://search.cpan.org/~creamyg/Sort-External-0.18/lib/Sort/External/Cookbook.pod#Descending_sort_order_using_the_%22bitwise_not%22_operator.

Cheers,

Larry


(This post was edited by 1arryb on Dec 18, 2009, 9:42 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives