|
Sort::PolySort -- general rules-based sorting of lists
|
Sort::PolySort -- general rules-based sorting of lists
This module is not included with the standard ActivePerl distribution. It is available as a separate download using PPM.
use Sort::PolySort;
$s=new Sort::PolySort; # defaults to 'name'
@people=('John Doe','Jane Doll','John Quasimodo Doe');
print join(", ",$s->sort(@people),"\n";
use Sort::PolySort;
$s=new Sort::PolySort('dateus'); # sets internal
@dates=$s->sort(@dates); # uses internal
$s->by('email'); # sets internal
@names=$s->sortby('name',@names); # overrides internal
@emails=$s->sort(@emails); # internal is still 'email'
This module provides methods to sort a list of strings based on
parsing the strings according to a configurable set of specifications.
The following methods are available:
- new
-
Creates a new polysort object. Takes optional arguement of initial
named spec set (defaults to name).
- by
-
Configures for the named set of sorting specs. Arguement is name of spec set.
- sort
-
Sorts by the previously-set (by new or by) specs. Arguement is a
list of items to sort. Returns the sorted list, leaving the original
unchanged.
- sortby
-
Sorts by the given (named) set of specs. The specs are only changed for
this particular sort, so future calls to sort will use whatever
specs were in effect before the sortby call. First arguement is
name of spec set, second arguement is a list of items to sort. Returns
the sorted list, leaving the original unchanged.
- get
-
Returns an associative array of the current sort specs. See NOTES.
- set
-
Sets the current sort specs to the given associative array. Specs not
appearing in the passed array retain their previous values, so this
method can be used along with get to keep state during a subroutine
call or to alter particular specs to get new sorting
results. Arguement is an associative array. See NOTES.
The following specifications are local to each Sort::PolySort object:
GLOBTOP
-
Lump last two levels together?
LEVELS
-
Number of levels to consider (0=all)
R2L
-
Count fields right to left?
NUMERIC
-
Do numerical sort?
CASE
-
Do case-sensitive sort?
DELIM1
-
Primary element delimiter (must not be null).
DELIM2
-
Secondary element delimiter (can be null).
The following order is followed to determine the keys used to sort the
given array:
-
DELIM2 (if given)
Remove up to leftmost (rightmost if R2L is true) occurance of DELIM2
(will be brought back later).
-
DELIM1
Split remainder at all occurances of DELIM1.
-
GLOBTOP (if true)
Rejoin left (right if R2L is true) 2 elements (always joined left-to-right,
regardless of R2L).
-
R2L (if true)
Reverse list of elements.
-
LEVELS
Store first LEVELS (all if =0) elements (last 2 considered as a
single element if GLOBTOP is true).
-
DELIM2 (if true)
Store string from left of DELIM2 as next element.
-
LEVELS (unless 0)
Rejoin remaining elements (in original order, regardless of R2L) and
store as next element.
The following (case-sensitive) names are used by new, by and
sortby to represent pre-defined sets of specs:
- datebr
-
by European (dd/mm/yy) date
- dateus
-
by US-style (mm/dd/yy) date
- email
-
by user for each machine (all parts of FQDN)
- email2
-
by user for each top-level domain (last 2 atoms)
- emaild
-
by user for each domain-name (next-to-last atom)
- ip
-
by numerical (aaa.bbb.ccc.ddd) ip address
- name
-
by last name/first name/middle name or initials
The following errors may occur:
- No specname given
-
by or sortby wasn't passed a specname.
- Never heard of specname ``foo''
-
new, by, or sortby was passed a name that was not in the list of
known specnames.
The whole parsing method is pretty perverse, but honestly was the first
thing that came to mind. It works, but is not very fast or extensible.
Enough interested folks mailed me that I wanted to get this out now, but
it's dyin' for a rewrite. So this is just a beta. The main interface will
remain the same, but the parser will be rewritten and the spec variables
changed. Accessor methods will change as a result (using %s=$s-get; ...
;$s->set(%s) will probably still work to save state, though). And accessor
methods wll be added so that new names spec sets can be added at runtime
or known ones modified. And new named spec sets will be added. And on and
on and on...
Daniel Macks <dmacks@netspace.org>
Copyright (c) 1996 by Daniel Macks. All rights reserved.
This module is free software; you may redistribute it
and/or modify it under the same terms as Perl itself.
|
Sort::PolySort -- general rules-based sorting of lists
|
|