You are not logged in.
Hi
I need some perl guru to help me out with changing the sort function in nzbperl (http://noisybox.net/computers/nzbperl/nzbperl.pl)
sub sortFilesBySubject {
my $quiet = shift;
my @fileset = @_;
if(!$nosort){
porlp("Sorting files by filename (subject)...", $quiet);
@fileset =
sort {
$a->{'name'} cmp $b->{'name'};
} @fileset;
porlp("finished.\n", $quiet);
}
return @fileset;
}
It sorts like this:
r00
r01
r02
..
rar
I'd like to sort like so:
rar
r00
r01
r02
..
Thanks for any advice :-)
Last edited by valvet (2010-11-08 14:51:00)
Offline
Like many problems, this one can be solved with a CPAN module. The Sort::Naturally module can do this for you. Here is an example of what you'd need to modify, with my own style of course:
use Sort::Naturally qw(ncmp); # put this at the top of your file
#...
sub sortFilesBySubject {
my ($quiet, @fileset) = @_;
return @fileset if $nosort;
porlp("Sorting files by filename (subject)...", $quiet);
@fileset = sort { ncmp( $a->{'name'}, $b->{'name'} ) } @fileset;
porlp("finished.\n", $quiet);
return @fileset;
}
I uploaded perl-sort-naturally to the AUR if you need a package for the Sort::Naturally module.
Offline
Like many problems, this one can be solved with a CPAN module. The Sort::Naturally module can do this for you. Here is an example of what you'd need to modify, with my own style of course:
use Sort::Naturally qw(ncmp); # put this at the top of your file #... sub sortFilesBySubject { my ($quiet, @fileset) = @_; return @fileset if $nosort; porlp("Sorting files by filename (subject)...", $quiet); @fileset = sort { ncmp( $a->{'name'}, $b->{'name'} ) } @fileset; porlp("finished.\n", $quiet); return @fileset; }
I uploaded perl-sort-naturally to the AUR if you need a package for the Sort::Naturally module.
Ah very nice, thanks alot :-). Just got home from a weekend of no-pc-time at the ocean (shocking).. will check it out later. Sleep is near..
Offline
Ok I just tried this (sleep can wait), and it doesn't appear to be sorting as .rar, .r01 - it takes .par2 files and puts them at the top tho.
I did as you wrote, defined qw at the top and replaced the original sorting function with yours. Any ideas? I'm unable to get any output of the actual sorting (@fileset contents) - I need a print_r function in Perl please :-)
Thanks
Offline
How is it sorting things? You can get close to print_r in php with Dumpvalue:
use Dumpvalue;
Dumpvalue->new->dumpValue( \@fileset );
But you might get too much information. An easier way is to just print a list of 'name' values:
print map { "$_->{name}\n" } @fileset;
I'm not sure why the post subjects wouldn't sort properly but honestly I don't know how they are formatted. A list of the 'name' values before being sorted and after might help me figure it out. I can't test nzbperl on my own I don't have a usenet server to use.
edit: to log to a file, printing is similar
open my $fh, '>>', 'debug.log' or die "open:$!";
print $fh map { "$_->{name}\n" } @fileset;
close $fh;
Last edited by juster (2010-05-23 17:13:49)
Offline
How is it sorting things? You can get close to print_r in php with Dumpvalue:
use Dumpvalue; Dumpvalue->new->dumpValue( \@fileset );
But you might get too much information. An easier way is to just print a list of 'name' values:
print map { "$_->{name}\n" } @fileset;
I'm not sure why the post subjects wouldn't sort properly but honestly I don't know how they are formatted. A list of the 'name' values before being sorted and after might help me figure it out. I can't test nzbperl on my own I don't have a usenet server to use.
edit: to log to a file, printing is similar
open my $fh, '>>', 'debug.log' or die "open:$!"; print $fh map { "$_->{name}\n" } @fileset; close $fh;
I'll check it out as soon as possible, thanks alot. NZB files are in plain XML format if that helps any.
<file poster="mom@nunya.com (abiv)" date="1274606179" subject="(#something Presents:) [03/25] - "file.name.par2" iTouch/iPhone (1/1)">
I asume it's parsing the filename within the "'s as this appears in all the NZB files I download. It's parsing the "subject" parameter of the <file> element.
Last edited by valvet (2010-05-23 17:27:59)
Offline
The debug is as follow ('ve changed the real filename as I'm unsure the rules regarding such info here):
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[1/1] - "filename.nzb" yEnc (1/1)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[01/23] - "filename.nfo" yEnc (1/1)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[02/23] - "filename.par2" yEnc (1/1)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[03/23] - "filename.r00" yEnc (1/40)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[04/23] - "filename.r01" yEnc (1/40)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[05/23] - "filename.r02" yEnc (1/40)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[06/23] - "filename.r03" yEnc (1/40)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[07/23] - "filename" yEnc (1/40)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[08/23] - "filename.r05" yEnc (1/40)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[09/23] - "filename.r06" yEnc (1/40)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[10/23] - "filename.r07" yEnc (1/40)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[11/23] - "filename.r08" yEnc (1/40)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[12/23] - "filename.r09" yEnc (1/40)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[13/23] - "filename.r10" yEnc (1/40)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[14/23] - "filename.r11" yEnc (1/6)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[15/23] - "filename.rar" yEnc (1/40)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[16/23] - "filename.sfv" yEnc (1/1)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[17/23] - "filename.srr" yEnc (1/1)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[18/23] - "filename.vol00+01.par2" yEnc (1/3)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[19/23] - "filename.vol01+02.par2" yEnc (1/5)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[20/23] - "filename.vol03+04.par2" yEnc (1/9)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[21/23] - "filename.vol07+08.par2" yEnc (1/17)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[22/23] - "filename.vol15+16.par2" yEnc (1/33)
[-]-[FULL]-[#something@EFNet]-[ subject_here ]-[23/23] - "filename.vol31+06.par2" yEnc (1/13)
Thanks again for taking the time to help with this.
Offline
I think I see the problem. You will have to extract the filename and compare both filenames. It seems to me the filename is always enclosed with double-quotes. I have made the function look for the last double-quoted string, just in case.
# Note: we assume the filename is the last double-quoted string.
sub subject_filename {
my ($subject) = @_;
return unless my ($filename) = $subject =~ /"([^"]+)"/g;
# Get the very last double-quoted string.
$filename = $1 while ( $subject =~ /"([^"]+)"/g );
return $filename;
}
sub by_filename {
my ($asubj, $bsubj) = map { $_->{'name'} } ( $a, $b );
my ($afile, $bfile) = map { subject_filename( $_ ) } ( $asubj, $bsubj );
# Do alphabetical sorting if we didn't extract both filenames...
return ( $asubj cmp $bsubj ) unless ( $afile && $bfile );
# Otherwise do "natural" sorting on the filenames...
return ncmp( $afile, $bfile );
}
Then inside sub sortBySubject:
@fileset = sort by_filename @fileset;
Note we are still using Sort::Naturally so don't remove the use Sort::Naturally line.
Offline
Thats pretty cool man, and working aswell - good job. Will study the examples and try learn a bit from it :-).
/bows
Offline