Sort-Fields-0.90/ 40755 1746 12 0 6464434521 12341 5ustar josephstaffSort-Fields-0.90/Makefile.PL100644 1746 12 355 6462322070 14364 0ustar josephstaffuse ExtUtils::MakeMaker; # See lib/ExtUtils/MakeMaker.pm for details of how to influence # the contents of the Makefile that is written. WriteMakefile( 'NAME' => 'Sort::Fields', 'VERSION_FROM' => 'Fields.pm', # finds $VERSION ); Sort-Fields-0.90/Changes100644 1746 12 350 6464434445 13714 0ustar josephstaffRevision history for Perl extension Sort::Fields. 0.01 Sat Jan 24 01:20:08 1998 - original version; created by h2xs 1.16 0.90 Fri Jan 30 13:33:13 MST 1998 - first reasonable beta version - incorporates a stable sorting option Sort-Fields-0.90/test.pl100644 1746 12 11542 6464433463 14001 0ustar josephstaff# Before `make install' is performed this script should be runnable with # `make test'. After `make install' it should work as `perl test.pl' ######################### We start with some black magic to print on failure. # Change 1..1 below to 1..last_test_to_print . # (It may become useful if the test is moved to ./t subdirectory.) BEGIN { $| = 1; print "1..1\n"; } END {print "not ok 1\n" unless $loaded;} use Sort::Fields; $loaded = 1; print "ok 1\n"; ######################### End of black magic. # Insert your test code below (better if it prints "ok 13" # (correspondingly "not ok 13") depending on the success of chunk 13 # of the test code): $^W = 1; my $n = 2; sub array_compare { my($a1, $a2) = @_; return 0 unless $#$a1 == $#$a2; for (my $i = 0; $i <= $#$a1; $i++) { return 0 unless $$a1[$i] eq $$a2[$i]; } 1; } sub test { print array_compare(@_) ? "ok $n\n" : "not ok $n\n"; $n++; } sub test_msg { my ($warn, $die) = ('', ''); local $SIG{__WARN__} = sub { $warn = shift }; local $SIG{__DIE__} = sub { $die = shift }; my $code = shift; eval $code; $warn =~ s/ at (?!.*\bat\b)[\w\W]*$//; $die =~ s/ at (?!.*\bat\b)[\w\W]*$//; if ($warn eq $_[0] and $die eq $_[1]) { print "ok $n\n"; } else { print "not ok $n\n"; print "code: $code"; print " warn: $warn\n" if $warn; print " die: $die\n" if $die; } $n++; } @data = ; # test warnings, errors for (qw(fieldsort stable_fieldsort make_fieldsort make_stable_fieldsort)) { test_msg qq{ $_(); }, "", "$_ requires argument(s)"; test_msg qq{ $_(1); }, "", "$_ field specifiers must be in anon array"; test_msg qq{ $_([]); }, "", "$_ must have at least one field specifier"; test_msg qq{ $_(['x']); }, "", "improperly formatted $_ column specifier 'x'"; test_msg qq{ $_(q/(/, ['1n']); }, "", "probable regexp error in $_ arg: /(/ /(/: unmatched () in regexp"; } for (qw(fieldsort stable_fieldsort)) { test_msg qq{ scalar $_([1], qw(a b c)); }, "fieldsort called in scalar or void context", ""; } # ascending numeric test( [fieldsort(['1n'], @data)], [map {$_->[0]} sort {$a->[1]<=>$b->[1]} map {[$_, split /\s+/]} @data] ); # ascending alpha test( [fieldsort([2], @data)], [map {$_->[0]} sort {$a->[2]cmp$b->[2]} map {[$_, split /\s+/]} @data] ); # ascending numeric test( [fieldsort(['3n'], @data)], [map {$_->[0]} sort {$a->[3]<=>$b->[3]} map {[$_, split /\s+/]} @data] ); # ascending numeric test( [fieldsort(['4n'], @data)], [map {$_->[0]} sort {$a->[4]<=>$b->[4]} map {[$_, split /\s+/]} @data] ); # descending numeric test( [fieldsort(['-1n'], @data)], [map {$_->[0]} sort {$b->[1]<=>$a->[1]} map {[$_, split /\s+/]} @data] ); # descending alpha test( [fieldsort([-2], @data)], [map {$_->[0]} sort {$b->[2]cmp$a->[2]} map {[$_, split /\s+/]} @data] ); # descending numeric test( [fieldsort(['-3n'], @data)], [map {$_->[0]} sort {$b->[3]<=>$a->[3]} map {[$_, split /\s+/]} @data] ); # descending numeric test( [fieldsort(['-4n'], @data)], [map {$_->[0]} sort {$b->[4]<=>$a->[4]} map {[$_, split /\s+/]} @data] ); # ascending alpha, then ascending numeric test( [fieldsort([1, '4n'], @data)], [map {$_->[0]} sort {$a->[1]cmp$b->[1] or $a->[4]<=>$b->[4]} map {[$_, split /\s+/]} @data] ); # ascending alpha, then descending numeric test( [fieldsort([1, '-4n'], @data)], [map {$_->[0]} sort {$a->[1]cmp$b->[1] or $b->[4]<=>$a->[4]} map {[$_, split /\s+/]} @data] ); # ascending alpha, then ascending alpha test( [fieldsort([2, 0], @data)], [map {$_->[0]} sort {$a->[2]cmp$b->[2] or $a->[0]cmp$b->[0]} map {[$_, split /\s+/]} @data] ); # ascending alpha, then descending numeric test( [fieldsort([2, '-0'], @data)], [map {$_->[0]} sort {$a->[2]cmp$b->[2] or $b->[0]cmp$a->[0]} map {[$_, split /\s+/]} @data] ); # stable, ascending numeric my $i = 0; test( [fieldsort(['-', '1n'], @data)], [map {$_->[0]} sort {$a->[2]<=>$b->[2] or $a->[1]<=>$b->[1]} map {[$_, $i++, split /\s+/]} @data] ); # stable, ascending numeric $i = 0; test( [stable_fieldsort(['1n'], @data)], [map {$_->[0]} sort {$a->[2]<=>$b->[2] or $a->[1]<=>$b->[1]} map {[$_, $i++, split /\s+/]} @data] ); # stable, ascending numeric, then descending alphabetic $i = 0; test( [stable_fieldsort(['1n', -2], @data)], [map {$_->[0]} sort {$a->[2]<=>$b->[2] or $b->[3]cmp$a->[3] or $a->[1]<=>$b->[1]} map {[$_, $i++, split /\s+/]} @data] ); $i = 0; __END__ 0 a 1 -4.5 0 a 2 -2.5 0 b 3 1e2 1 b 4 123456 1 b 5 1e3 1 b 6 2e5 0 b 7 .00001 0 a 8 .00002 1 a 9 -.234e2 1 b 10 1e-1 0 a 12 1e-2 0 a 13 .123 0 a 14 .234 0 b 15 1.234 0 a 16 12345.6789 1 a 17 12345.6788 1 a 18 12345.6787 1 a 19 -2222 0 b 20 -2223 0 b 21 -1e1 1 b 22 -1e-1 1 b 23 -2e2 0 b 24 -2e-2 0 b 25 -3e3 0 b 26 -3e-3 0 b 27 123345.123234 0 a 28 123345.123235 0 a 29 123345.123233 0 a 30 -4.6 0 b 31 -4.7 0 b 32 -4.8 1 b 33 -4.5e1 1 a 34 1.23 1 a 35 2.345 1 b 36 345.456 1 a 37 45678.67567 0 b 38 23423422.34234234 1 a 39 123124123 Sort-Fields-0.90/Fields.pm100644 1746 12 22734 6464433527 14237 0ustar josephstaffpackage Sort::Fields; use strict; use vars qw($VERSION @ISA @EXPORT); require Exporter; require 5.003_03; @ISA = qw(Exporter); # Items to export into callers namespace by default. Note: do not export # names by default without a very good reason. Use EXPORT_OK instead. # Do not simply export all your public functions/methods/constants. @EXPORT = qw( make_fieldsort fieldsort make_stable_fieldsort stable_fieldsort ); $VERSION = '0.90'; use Carp; sub make_fieldsort { my $selfname; if ((caller)[0] eq 'Sort::Fields') { ($selfname) = (caller 1)[3] =~ /([^:]*)$/; } else { $selfname = 'make_fieldsort' }; unless (@_) { croak "$selfname requires argument(s)"; } my ($sep, $cols); if (ref $_[0]) { $sep = '\\s+' } else { $sep = shift; } unless (ref($cols = shift) eq 'ARRAY') { croak "$selfname field specifiers must be in anon array"; } my (@sortcode, @col); my $level = 1; my $maxcol = -1; my $stable = 0; if (@$cols and $$cols[0] eq '-') { shift @$cols; $stable = 1; } unless (@$cols) { croak "$selfname must have at least one field specifier"; } for (@$cols) { unless (/^-?\d+n?$/) { croak "improperly formatted $selfname column specifier '$_'"; } my ($a, $b) = /^-/ ? qw(b a) : qw(a b); my $op = /n$/ ? '<=>' : 'cmp'; my ($col) = /^-?(\d+)/; if ($col == 0) { # column 0 gives the entire string push @sortcode, "\$${a}->[0] $op \$${b}->[0]"; next; } push @col, (/(\d+)/)[0] - 1; $maxcol = $col[-1] if $maxcol < $col[-1]; if ($stable) { # indices are offset by 1 in this case my $levp1 = $level + 1; push @sortcode, "\$${a}->[$levp1] $op \$${b}->[$levp1]"; } else { push @sortcode, "\$${a}->[$level] $op \$${b}->[$level]"; } $level++; } # have to check this all by itself, since if there's a regex # error it won't show up until the sub is called (urk!) eval '"" =~ /$sep/'; if ($@) { croak "probable regexp error in $selfname arg: /$sep/\n$@"; } my $splitfunc; $splitfunc = eval 'sub { (split /$sep/o, $_, $maxcol + 2)[@col] } '; if ($@) { die "eval failed in $selfname (internal error?)\n$@"; } my $sortcode = join " or ", @sortcode; my $sub; if ($stable) { my $i; # the $i for the stable sort closure $sub = eval qq{ sub { if (\$^W and not wantarray) { carp "fieldsort called in scalar or void context"; } \$i = 0; # reset counter in case reusing this closure map \$_->[0], sort { $sortcode or \$a->[1] <=> \$b->[1] } map [\$_, \$i++, \$splitfunc->(\$_)], \@_; } } } else { $sub = eval qq{ sub { if (\$^W and not wantarray) { carp "fieldsort called in scalar or void context"; } map \$_->[0], sort { $sortcode } map [\$_, \$splitfunc->(\$_)], \@_; } } } if ($@) { die "eval failed in $selfname (internal error?)\n$@"; } $sub; } sub make_stable_fieldsort { unless (@_) { croak "make_stable_fieldsort requires argument(s)"; } if (ref $_[0] eq 'ARRAY') { unshift @{$_[0]}, '-'; } elsif (@_ > 1 and ref $_[1] eq 'ARRAY') { unshift @{$_[1]}, '-'; } make_fieldsort @_; } sub fieldsort { unless (@_) { croak "fieldsort requires argument(s)"; } my ($sep, $cols); if (ref $_[0]) { $sep = '\\s+' } else { $sep = shift; } $cols = shift; make_fieldsort($sep, $cols)->(@_); } sub stable_fieldsort { unless (@_) { croak "stable_fieldsort requires argument(s)"; } my ($sep, $cols); if (ref $_[0] eq 'ARRAY') { $sep = '\\s+'; unshift @{$_[0]}, '-'; } elsif (@_ > 1 and ref $_[1] eq 'ARRAY') { $sep = shift; unshift @{$_[1]}, '-'; } $cols = shift; make_fieldsort($sep, $cols)->(@_); } # Autoload methods go after =cut, and are processed by the autosplit program. 1; __END__ =head1 NAME Sort::Fields - Sort lines containing delimited fields =head1 SYNOPSIS use Sort::Fields; @sorted = fieldsort [3, '2n'], @lines; @sorted = fieldsort '\+', [-1, -3, 0], @lines; $sort_3_2n = make_fieldsort [3, '2n'], @lines; @sorted = $sort_3_2n->(@lines); =head1 DESCRIPTION Sort::Fields provides a general purpose technique for efficiently sorting lists of lines that contain data separated into fields. Sort::Fields automatically imports two subroutines, C and C, and two variants, C and C. C generates a sorting subroutine and returns a reference to it. C is a wrapper for the C subroutine. The first argument to make_fieldsort is a delimiter string, which is used as a regular expression argument for a C operator. The delimiter string is optional. If it is not supplied, make_fieldsort splits each line using C. The second argument is an array reference containing one or more field specifiers. The specifiers indicate what fields in the strings will be used to sort the data. The specifier "1" indicates the first field, "2" indicates the second, and so on. A negative specifier like "-2" means to sort on the second field in reverse (descending) order. To indicate a numeric rather than alphabetic comparison, append "n" to the specifier. A specifier of "0" means the entire string ("-0" means the entire string, in reverse order). The order in which the specifiers appear is the order in which they will be used to sort the data. The primary key is first, the secondary key is second, and so on. C is roughly equivalent to C(@data)>. Avoid calling fieldsort repeatedly with the same sort specifiers. If you need to use a particular sort more than once, it is more efficient to call C once and reuse the subroutine it returns. C and C are like their "unstable" counterparts, except that the items that compare the same are maintained in their original order. =head1 EXAMPLES Some sample data (in array C<@data>): 123 asd 1.22 asdd 32 ewq 2.32 asdd 43 rewq 2.12 ewet 51 erwt 34.2 ewet 23 erww 4.21 ewet 91 fdgs 3.43 ewet 123 refs 3.22 asdd 123 refs 4.32 asdd # alpha sort on column 1 print fieldsort [1], @data; 123 asd 1.22 asdd 123 refs 3.22 asdd 123 refs 4.32 asdd 23 erww 4.21 ewet 32 ewq 2.32 asdd 43 rewq 2.12 ewet 51 erwt 34.2 ewet 91 fdgs 3.43 ewet # numeric sort on column 1 print fieldsort ['1n'], @data; 23 erww 4.21 ewet 32 ewq 2.32 asdd 43 rewq 2.12 ewet 51 erwt 34.2 ewet 91 fdgs 3.43 ewet 123 asd 1.22 asdd 123 refs 3.22 asdd 123 refs 4.32 asdd # reverse numeric sort on column 1 print fieldsort ['-1n'], @data; 123 asd 1.22 asdd 123 refs 3.22 asdd 123 refs 4.32 asdd 91 fdgs 3.43 ewet 51 erwt 34.2 ewet 43 rewq 2.12 ewet 32 ewq 2.32 asdd 23 erww 4.21 ewet # alpha sort on column 2, then alpha on entire line print fieldsort [2, 0], @data; 123 asd 1.22 asdd 51 erwt 34.2 ewet 23 erww 4.21 ewet 32 ewq 2.32 asdd 91 fdgs 3.43 ewet 123 refs 3.22 asdd 123 refs 4.32 asdd 43 rewq 2.12 ewet # alpha sort on column 4, then numeric on column 1, then reverse # numeric on column 3 print fieldsort [4, '1n', '-3n'], @data; 32 ewq 2.32 asdd 123 refs 4.32 asdd 123 refs 3.22 asdd 123 asd 1.22 asdd 23 erww 4.21 ewet 43 rewq 2.12 ewet 51 erwt 34.2 ewet 91 fdgs 3.43 ewet # now, splitting on either literal period or whitespace # sort numeric on column 4 (fractional part of decimals) then # numeric on column 3 (whole part of decimals) print fieldsort '(?:\.|\s+)', ['4n', '3n'], @data; 51 erwt 34.2 ewet 43 rewq 2.12 ewet 23 erww 4.21 ewet 123 asd 1.22 asdd 123 refs 3.22 asdd 32 ewq 2.32 asdd 123 refs 4.32 asdd 91 fdgs 3.43 ewet # alpha sort on column 4, then numeric on the entire line # NOTE: produces warnings under -w print fieldsort [4, '0n'], @data; 32 ewq 2.32 asdd 123 asd 1.22 asdd 123 refs 3.22 asdd 123 refs 4.32 asdd 23 erww 4.21 ewet 43 rewq 2.12 ewet 51 erwt 34.2 ewet 91 fdgs 3.43 ewet # stable alpha sort on column 4 (maintains original relative order # among items that compare the same) print stable_fieldsort [4], @data; 123 asd 1.22 asdd 32 ewq 2.32 asdd 123 refs 3.22 asdd 123 refs 4.32 asdd 43 rewq 2.12 ewet 51 erwt 34.2 ewet 23 erww 4.21 ewet 91 fdgs 3.43 ewet =head1 BUGS Some rudimentary tests now. Perhaps something should be done to catch things like: fieldsort '.', [1, 2], @lines; C<'.'> translates to C -- probably not what you want. Passing blank lines and/or lines containing the wrong kind of data (alphas instead of numbers) can result in copious warning messages under C<-w>. If the regexp contains memory parentheses (C<(...)> rather than C<(?:...)>), split will function in "delimiter retention" mode, capturing the contents of the parentheses as well as the stuff between the delimiters. I could imagine how this could be useful, but on the other hand I could also imagine how it could be confusing if encountered unexpectedly. Caveat sortor. Not really a bug, but if you are planning to sort a large text file, consider using sort(1). Unless, of course, your operating system doesn't have sort(1). =head1 AUTHOR Joseph N. Hall, joseph@5sigma.com =head1 SEE ALSO perl(1). =cut Sort-Fields-0.90/README100644 1746 12 516 6464434347 13306 0ustar josephstaffNAME Sort::Fields - Sort lines containing delimited fields SYNOPSIS use Sort::Fields; @sorted = fieldsort [3, '2n'], @lines; @sorted = fieldsort '\+', [-1, -3, 0], @lines; $sort_3_2n = make_fieldsort [3, '2n'], @lines; @sorted = $sort_3_2n->(@lines); AUTHOR Joseph N. Hall, joseph@5sigma.com Sort-Fields-0.90/MANIFEST100644 1746 12 66 6464434362 13534 0ustar josephstaffChanges Fields.pm MANIFEST Makefile.PL test.pl README