Sort-Fields-1.001/ 000755 000765 000024 00000000000 13774725341 014121 5 ustar 00brian staff 000000 000000 Sort-Fields-1.001/LICENSE 000644 000765 000024 00000017403 13774725340 015132 0 ustar 00brian staff 000000 000000 The Sort::Fields module is licensed under the same terms as perl
itself, under the Artistic License 2.0.
Artistic License 2.0
Copyright (c) 2000-2006, The Perl Foundation.
http://www.perlfoundation.org/artistic_license_2_0
Preamble
This license establishes the terms under which a given free software
Package may be copied, modified, distributed, and/or redistributed.
The intent is that the Copyright Holder maintains some artistic
control over the development of that Package while still keeping the
Package available as open source and free software.
You are always permitted to make arrangements wholly outside of this
license directly with the Copyright Holder of a given Package. If the
terms of this license do not permit the full use that you propose to
make of the Package, you should contact the Copyright Holder and seek
a different licensing arrangement.
Definitions
"Copyright Holder" means the individual(s) or organization(s) named in
the copyright notice for the entire Package.
"Contributor" means any party that has contributed code or other
material to the Package, in accordance with the Copyright Holder's
procedures.
"You" and "your" means any person who would like to copy, distribute,
or modify the Package.
"Package" means the collection of files distributed by the Copyright
Holder, and derivatives of that collection and/or of those files. A
given Package may consist of either the Standard Version, or a
Modified Version.
"Distribute" means providing a copy of the Package or making it
accessible to anyone else, or in the case of a company or
organization, to others outside of your company or organization.
"Distributor Fee" means any fee that you charge for Distributing this
Package or providing support for this Package to another party. It
does not mean licensing fees.
"Standard Version" refers to the Package if it has not been modified,
or has been modified only in ways explicitly requested by the
Copyright Holder.
"Modified Version" means the Package, if it has been changed, and such
changes were not explicitly requested by the Copyright Holder.
"Original License" means this Artistic License as Distributed with the
Standard Version of the Package, in its current version or as it may
be modified by The Perl Foundation in the future.
"Source" form means the source code, documentation source, and
configuration files for the Package.
"Compiled" form means the compiled bytecode, object code, binary, or
any other form resulting from mechanical transformation or translation
of the Source form.
Permission for Use and Modification Without Distribution
(1) You are permitted to use the Standard Version and create and use
Modified Versions for any purpose without restriction, provided that
you do not Distribute the Modified Version.
Permissions for Redistribution of the Standard Version
(2) You may Distribute verbatim copies of the Source form of the
Standard Version of this Package in any medium without restriction,
either gratis or for a Distributor Fee, provided that you duplicate
all of the original copyright notices and associated disclaimers. At
your discretion, such verbatim copies may or may not include a
Compiled form of the Package.
(3) You may apply any bug fixes, portability changes, and other
modifications made available from the Copyright Holder. The resulting
Package will still be considered the Standard Version, and as such
will be subject to the Original License.
Distribution of Modified Versions of the Package as Source
(4) You may Distribute your Modified Version as Source (either gratis
or for a Distributor Fee, and with or without a Compiled form of the
Modified Version) provided that you clearly document how it differs
from the Standard Version, including, but not limited to, documenting
any non-standard features, executables, or modules, and provided that
you do at least ONE of the following:
(a) make the Modified Version available to the Copyright Holder of the
Standard Version, under the Original License, so that the Copyright
Holder may include your modifications in the Standard Version.
(b) ensure that installation of your Modified Version does not prevent
the user installing or running the Standard Version. In addition, the
Modified Version must bear a name that is different from the name of
the Standard Version.
(c) allow anyone who receives a copy of the Modified Version to make
the Source form of the Modified Version available to others under
(i) the Original License or
(ii) a license that permits the licensee to freely copy, modify and
redistribute the Modified Version using the same licensing terms that
apply to the copy that the licensee received, and requires that the
Source form of the Modified Version, and of any works derived from it,
be made freely available in that license fees are prohibited but
Distributor Fees are allowed.
Distribution of Compiled Forms of the Standard Version or Modified
Versions without the Source
(5) You may Distribute Compiled forms of the Standard Version without
the Source, provided that you include complete instructions on how to
get the Source of the Standard Version. Such instructions must be
valid at the time of your distribution. If these instructions, at any
time while you are carrying out such distribution, become invalid, you
must provide new instructions on demand or cease further distribution.
If you provide valid instructions or cease distribution within thirty
days after you become aware that the instructions are invalid, then
you do not forfeit any of your rights under this license.
(6) You may Distribute a Modified Version in Compiled form without the
Source, provided that you comply with Section 4 with respect to the
Source of the Modified Version.
Aggregating or Linking the Package
(7) You may aggregate the Package (either the Standard Version or
Modified Version) with other packages and Distribute the resulting
aggregation provided that you do not charge a licensing fee for the
Package. Distributor Fees are permitted, and licensing fees for other
components in the aggregation are permitted. The terms of this license
apply to the use and Distribution of the Standard or Modified Versions
as included in the aggregation.
(8) You are permitted to link Modified and Standard Versions with
other works, to embed the Package in a larger work of your own, or to
build stand-alone binary or bytecode versions of applications that
include the Package, and Distribute the result without restriction,
provided the result does not expose a direct interface to the Package.
Items That are Not Considered Part of a Modified Version
(9) Works (including, but not limited to, modules and scripts) that
merely extend or make use of the Package, do not, by themselves, cause
the Package to be a Modified Version. In addition, such works are not
considered parts of the Package itself, and are not subject to the
terms of this license.
General Provisions
(10) Any use, modification, and distribution of the Standard or
Modified Versions is governed by this Artistic License. By using,
modifying or distributing the Package, you accept this license. Do not
use, modify, or distribute the Package, if you do not accept this
license.
(11) If your Modified Version has been derived from a Modified Version
made by someone other than you, you are nevertheless required to
ensure that your Modified Version complies with the requirements of
this license.
(12) This license does not grant you the right to use any trademark,
service mark, tradename, or logo of the Copyright Holder.
(13) This license includes the non-exclusive, worldwide,
free-of-charge patent license to make, have made, use, offer to sell,
sell, import and otherwise transfer the Package with respect to any
patent claims licensable by the Copyright Holder that are necessarily
infringed by the Package. If you institute patent litigation
Sort-Fields-1.001/Changes 000644 000765 000024 00000001357 13774725340 015421 0 ustar 00brian staff 000000 000000 Revision history for Perl extension Sort::Fields
1.001 2021-01-04T23:52:23Z
* rt.cpan.org will shut down in March 2021, so it's a good time
to give this module different meta data
* brian d foy is maintaining the distro, but this is limited to
making it work with current services. The code is all Joseph Hall's.
No future development or features is planned
* The test.pl is moved, with slight modifications, to t/test.t and
Test::More
* If you experience problems, report them in GitHub:
https://github.com/briandfoy/sort-fields
* If you'd like to take over the module, open an issue.
0.90 - 1998-01-30
* first reasonable beta version
* incorporates a stable sorting option
0.01 - 1998-01-24
* original version; created by h2xs 1.16
Sort-Fields-1.001/MANIFEST 000644 000765 000024 00000000501 13774725341 015246 0 ustar 00brian staff 000000 000000 Changes
INSTALL.SKIP
lib/Sort/Fields.pm
LICENSE
Makefile.PL
MANIFEST This list of files
MANIFEST.SKIP
README.pod
t/test.t
t/test_manifest
xt/changes.t
META.yml Module YAML meta-data (added by MakeMaker)
META.json Module JSON meta-data (added by MakeMaker)
Sort-Fields-1.001/t/ 000755 000765 000024 00000000000 13774725341 014364 5 ustar 00brian staff 000000 000000 Sort-Fields-1.001/xt/ 000755 000765 000024 00000000000 13774725341 014554 5 ustar 00brian staff 000000 000000 Sort-Fields-1.001/README.pod 000644 000765 000024 00000007722 13774725340 015571 0 ustar 00brian staff 000000 000000 =pod
=encoding utf8
=for HTML
=for HTML
=for HTML
=for HTML
=for HTML
=for HTML
=for HTML
=head1 The Sort::Fields module
This is the I for the L Perl module. You're probably
looking at this because you don't know where else to find what you're
looking for. Read this once and you might never have to read one again
for any Perl module.
=head2 Documentation
To read about L, look at the embedded documentation
in the module itself. Inside the distribution, you can format it
with L:
% perldoc lib/Sort/Fields.pm
If you have already installed the module, you can specify the module
name instead of the file location:
% perldoc Sort::Fields
You can read the documentation and inspect the meta data at
L.
The standard module documentation has example uses in the SYNOPSIS
section, but you can also look in the I directory (if it's
there), or look at the test files in I.
=head2 Installation
You can install this module with a CPAN client, which will resolve
and install the dependencies:
% cpan Sort::Fields
% cpanm Sort::Fields
You can also install directly from the distribution directory, which
will also install the dependencies:
% cpan .
% cpanm .
You could install just this module manually:
% perl Makefile.PL
% make
% make test
% make install
You probably don't want to do that unless you're fiddling with the
module and only want to run the tests without installing anything.
=head2 Source location
The meta data, such as the source repository and bug tracker, is in
I or the I files it creates. You can find that on
those CPAN web interfaces, but you can also look at files directly in
the source repository:
=over 4
=item * L
=back
If you find a problem, file a ticket in the L:
=over 4
=item * L
=back
=head2 Getting help
Although I'm happy to hear from module users in private email,
that's the best way for me to forget to do something.
Besides the issue trackers, you can find help at
L or
L, both of which have many
competent Perlers who can answer your question, almost in real time.
They might not know the particulars of this module, but they can help
you diagnose your problem.
You might like to read L.
=head2 Copyright and License
You should have received a I file, but the license is also noted
in the module files. About the only thing you can't do is pretend that
you wrote code that you didn't.
=head2 Good luck!
Enjoy,
brian d foy, bdfoy@cpan.org
=cut
Sort-Fields-1.001/MANIFEST.SKIP 000644 000765 000024 00000002027 13774725340 016017 0 ustar 00brian staff 000000 000000
#!start included /usr/local/perls/perl-5.20.0/lib/5.20.0/ExtUtils/MANIFEST.SKIP
# Avoid version control files.
\bRCS\b
\bCVS\b
\bSCCS\b
,v$
\B\.svn\b
\B\.git\b
\B\.gitignore\b
\b_darcs\b
\B\.cvsignore$
# Avoid VMS specific MakeMaker generated files
\bDescrip.MMS$
\bDESCRIP.MMS$
\bdescrip.mms$
# Avoid Makemaker generated and utility files.
\bMANIFEST\.bak
\bMakefile$
\bblib/
\bMakeMaker-\d
\bpm_to_blib\.ts$
\bpm_to_blib$
\bblibdirs\.ts$ # 6.18 through 6.25 generated this
# Avoid Module::Build generated and utility files.
\bBuild$
\b_build/
\bBuild.bat$
\bBuild.COM$
\bBUILD.COM$
\bbuild.com$
# Avoid temp and backup files.
~$
\.old$
\#$
\b\.#
\.bak$
\.tmp$
\.#
\.rej$
# Avoid OS-specific files/dirs
# Mac OSX metadata
\B\.DS_Store
# Mac OSX SMB mount metadata files
\B\._
# Avoid Devel::Cover and Devel::CoverX::Covered files.
\bcover_db\b
\bcovered\b
# Avoid MYMETA files
^MYMETA\.
#!end included /usr/local/perls/perl-5.20.0/lib/5.20.0/ExtUtils/MANIFEST.SKIP
\.travis\.yml
\.?appveyor.yml
\.releaserc
\.lwpcookies
\.github
Sort-Fields-1.001/META.yml 000664 000765 000024 00000001401 13774725341 015370 0 ustar 00brian staff 000000 000000 ---
abstract: 'Sort lines containing delimited fields'
author:
- 'Joseph N. Hall '
build_requires:
Test::More: '1'
configure_requires:
ExtUtils::MakeMaker: '6.64'
File::Spec::Functions: '0'
dynamic_config: 1
generated_by: 'ExtUtils::MakeMaker version 7.34, CPAN::Meta::Converter version 2.150010'
license: perl
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
version: '1.4'
name: Sort-Fields
no_index:
directory:
- t
- inc
requires:
perl: '5.008'
resources:
bugtracker: https://github.com/briandfoy/sort-fields/issues
homepage: https://github.com/briandfoy/sort-fields
repository: https://github.com/briandfoy/sort-fields.git
version: '1.001'
x_serialization_backend: 'CPAN::Meta::YAML version 0.018'
Sort-Fields-1.001/lib/ 000755 000765 000024 00000000000 13774725341 014667 5 ustar 00brian staff 000000 000000 Sort-Fields-1.001/Makefile.PL 000644 000765 000024 00000006221 13774725340 016073 0 ustar 00brian staff 000000 000000 package Sort::Fields;
use strict;
use warnings;
=encoding utf8
=head1 The build file for Sort::Fields
This build file is a modulino; it works as both a build script and
a module.
To build the distribution, run this file normally:
% perl Makefile.PL
But, it's more interesting than that. You can load it with C
and call C to get the data structure it passes to
C:
my $package = require '/path/to/Makefile.PL';
my $arguments = $package->arguments;
Note that C-ing a file makes an entry in C<%INC> for exactly
that name. If you try to C another file with the same name,
even from a different path, C thinks it has already loaded
the file. As such, I recommend you always require the full path to the
file.
The return value of the C is a package name (in this case,
the name of the main module. Use that to call the C method.
Even if this distribution needs a higher version of Perl, this bit
only needs v5.8. You can play with the data structure with a primitive
Perl.
=cut
use File::Spec::Functions qw(catfile);
my $module = __PACKAGE__;
( my $dist = $module ) =~ s/::/-/g;
my $github = 'https://github.com/briandfoy/sort-fields';
my $main_file = catfile( 'lib', split /::/, "$module.pm" );
my %WriteMakefile = (
'MIN_PERL_VERSION' => '5.008',
'NAME' => $module,
'ABSTRACT_FROM' => $main_file,
'VERSION_FROM' => $main_file,
'LICENSE' => 'perl',
'AUTHOR' => 'Joseph N. Hall ',
'CONFIGURE_REQUIRES' => {
'ExtUtils::MakeMaker' => '6.64',
'File::Spec::Functions' => '0',
},
'BUILD_REQUIRES' => {
},
'TEST_REQUIRES' => {
'Test::More' => '1',
},
'PREREQ_PM' => {
},
'META_MERGE' => {
'meta-spec' => { version => 2 },
resources => {
repository => {
type => 'git',
url => "$github.git",
web => $github,
},
bugtracker => {
web => "$github/issues",
},
homepage => $github,
},
},
clean => { FILES => "$dist-*" },
);
sub arguments { \%WriteMakefile }
do_it() unless caller;
sub do_it {
require File::Spec;
my $MM ='ExtUtils::MakeMaker';
my $MM_version =
eval{ "$MM " . $WriteMakefile{'CONFIGURE_REQUIRES'}{'ExtUtils::MakeMaker'} }
||
"$MM 6.64";
eval "use $MM_version; 1" or die "Could not load $MM_version: $@";
eval "use Test::Manifest 1.21"
if -e File::Spec->catfile( qw(t test_manifest) );
my $arguments = arguments();
my $minimum_perl = $arguments->{MIN_PERL_VERSION} || '5.008';
eval "require $minimum_perl;" or die $@;
WriteMakefile( %$arguments );
}
no warnings;
__PACKAGE__;
sub arguments { \%WriteMakefile }
do_it() unless caller;
sub do_it {
require File::Spec;
my $MM ='ExtUtils::MakeMaker';
my $MM_version =
eval{ "$MM " . $WriteMakefile{'CONFIGURE_REQUIRES'}{'ExtUtils::MakeMaker'} }
||
"$MM 6.64";
eval "use $MM_version; 1" or die "Could not load $MM_version: $@";
eval "use Test::Manifest 1.21"
if -e File::Spec->catfile( qw(t test_manifest) );
my $arguments = arguments();
my $minimum_perl = $arguments->{MIN_PERL_VERSION} || '5.008';
eval "require $minimum_perl;" or die $@;
WriteMakefile( %$arguments );
}
no warnings;
__PACKAGE__;
Sort-Fields-1.001/INSTALL.SKIP 000644 000765 000024 00000000156 13774725340 015720 0 ustar 00brian staff 000000 000000 README\.pod
README.*
# things that might be in local directories after fooling
# around with them
\.DS_Store
Sort-Fields-1.001/META.json 000664 000765 000024 00000002557 13774725341 015555 0 ustar 00brian staff 000000 000000 {
"abstract" : "Sort lines containing delimited fields",
"author" : [
"Joseph N. Hall "
],
"dynamic_config" : 1,
"generated_by" : "ExtUtils::MakeMaker version 7.34, CPAN::Meta::Converter version 2.150010",
"license" : [
"perl_5"
],
"meta-spec" : {
"url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",
"version" : 2
},
"name" : "Sort-Fields",
"no_index" : {
"directory" : [
"t",
"inc"
]
},
"prereqs" : {
"build" : {
"requires" : {}
},
"configure" : {
"requires" : {
"ExtUtils::MakeMaker" : "6.64",
"File::Spec::Functions" : "0"
}
},
"runtime" : {
"requires" : {
"perl" : "5.008"
}
},
"test" : {
"requires" : {
"Test::More" : "1"
}
}
},
"release_status" : "stable",
"resources" : {
"bugtracker" : {
"web" : "https://github.com/briandfoy/sort-fields/issues"
},
"homepage" : "https://github.com/briandfoy/sort-fields",
"repository" : {
"type" : "git",
"url" : "https://github.com/briandfoy/sort-fields.git",
"web" : "https://github.com/briandfoy/sort-fields"
}
},
"version" : "1.001",
"x_serialization_backend" : "JSON::PP version 4.02"
}
Sort-Fields-1.001/lib/Sort/ 000755 000765 000024 00000000000 13774725341 015616 5 ustar 00brian staff 000000 000000 Sort-Fields-1.001/lib/Sort/Fields.pm 000644 000765 000024 00000022621 13774725340 017364 0 ustar 00brian staff 000000 000000 package Sort::Fields;
use strict;
use vars qw($VERSION @EXPORT);
use Exporter qw(import);
require 5.003_03;
;
# 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 = '1.001';
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)->(@_);
}
1;
__END__
=encoding utf8
=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\s+/>.
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, C<< >>
=head1 SEE ALSO
perl(1).
=cut
Sort-Fields-1.001/xt/changes.t 000644 000765 000024 00000000203 13774725340 016343 0 ustar 00brian staff 000000 000000 use Test::More;
eval 'use Test::CPAN::Changes';
plan skip_all => 'Test::CPAN::Changes required for this test' if $@;
changes_ok();
Sort-Fields-1.001/t/test.t 000644 000765 000024 00000011457 13774725340 015537 0 ustar 00brian staff 000000 000000 use utf8;
use strict;
use warnings;
use Test::More 0.94;
BEGIN { use_ok( 'Sort::Fields' ) }
####################################################################
# test warnings, errors
sub test_msg {
my( $code, $warn_expected, $die_expected ) = @_;
my ($warn, $die) = ('', '');
local $SIG{__WARN__} = sub { $warn = shift };
local $SIG{__DIE__} = sub { $die = shift };
eval $code;
$warn =~ s/ at (?!.*\bat\b)[\w\W]*$//;
$die =~ s/ at (?!.*\bat\b)[\w\W]*$//;
subtest $code => sub {
is( $warn, $warn_expected, "warn message is correct" ) or diag(
"code: $code\nwarn: $warn\n" );
is( $die, $die_expected, "die message is correct" ) or diag(
"code: $code\nwarn: $die\n" );
};
}
# warnings change from version to version, so let this perl
# tell you what it is now.
my $regex_warning = do {
eval "m/(/";
my $die = $@;
$die =~ s/ at (?!.*\bat\b)[\w\W]*$//;
$die;
};
#diag( "regex warning is $regex_warning" );
foreach ( 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: /(/\n$regex_warning";
}
foreach ( qw(fieldsort stable_fieldsort) ) {
local $^W = 1; # This is definitely old school,
# but this is part of the condition to trigger the message
test_msg qq{ scalar $_([1], qw(a b c)); }, "fieldsort called in scalar or void context", "";
}
####################################################################
# test that it all works
my @data = ;
is_deeply(
[fieldsort(['1n'], @data)],
[map {$_->[0]} sort {$a->[1]<=>$b->[1]} map {[$_, split /\s+/]} @data],
"ascending numeric"
);
is_deeply(
[fieldsort([2], @data)],
[map {$_->[0]} sort {$a->[2]cmp$b->[2]} map {[$_, split /\s+/]} @data],
"ascending alpha"
);
is_deeply(
[fieldsort(['3n'], @data)],
[map {$_->[0]} sort {$a->[3]<=>$b->[3]} map {[$_, split /\s+/]} @data],
"ascending numeric"
);
is_deeply(
[fieldsort(['4n'], @data)],
[map {$_->[0]} sort {$a->[4]<=>$b->[4]} map {[$_, split /\s+/]} @data],
"ascending numeric"
);
is_deeply(
[fieldsort(['-1n'], @data)],
[map {$_->[0]} sort {$b->[1]<=>$a->[1]} map {[$_, split /\s+/]} @data],
"descending numeric"
);
is_deeply(
[fieldsort([-2], @data)],
[map {$_->[0]} sort {$b->[2]cmp$a->[2]} map {[$_, split /\s+/]} @data],
"descending alpha"
);
is_deeply(
[fieldsort(['-3n'], @data)],
[map {$_->[0]} sort {$b->[3]<=>$a->[3]} map {[$_, split /\s+/]} @data],
"descending numeric"
);
is_deeply(
[fieldsort(['-4n'], @data)],
[map {$_->[0]} sort {$b->[4]<=>$a->[4]} map {[$_, split /\s+/]} @data],
"descending numeric"
);
is_deeply(
[fieldsort([1, '4n'], @data)],
[map {$_->[0]} sort {$a->[1]cmp$b->[1] or $a->[4]<=>$b->[4]} map {[$_, split /\s+/]} @data],
"ascending alpha, then ascending numeric"
);
is_deeply(
[fieldsort([1, '-4n'], @data)],
[map {$_->[0]} sort {$a->[1]cmp$b->[1] or $b->[4]<=>$a->[4]} map {[$_, split /\s+/]} @data],
"ascending alpha, then descending numeric"
);
is_deeply(
[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 ascending alpha"
);
is_deeply(
[fieldsort([2, '-0'], @data)],
[map {$_->[0]} sort {$a->[2]cmp$b->[2] or $b->[0]cmp$a->[0]} map {[$_, split /\s+/]} @data],
"ascending alpha, then descending numeric"
);
{
my $i = 0;
is_deeply(
[fieldsort(['-', '1n'], @data)],
[map {$_->[0]} sort {$a->[2]<=>$b->[2] or $a->[1]<=>$b->[1]} map {[$_, $i++, split /\s+/]} @data],
"stable, ascending numeric"
);
}
{
my $i = 0;
is_deeply(
[stable_fieldsort(['1n'], @data)],
[map {$_->[0]} sort {$a->[2]<=>$b->[2] or $a->[1]<=>$b->[1]} map {[$_, $i++, split /\s+/]} @data],
"stable, ascending numeric"
);
}
{
my $i = 0;
is_deeply(
[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],
"stable, ascending numeric, then descending alphabetic"
);
}
done_testing();
__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-1.001/t/test_manifest 000644 000765 000024 00000000007 13774725340 017150 0 ustar 00brian staff 000000 000000 test.t