.\" Automatically generated by Pod::Man 2.25 (Pod::Simple 3.16) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .ie \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .el \{\ . de IX .. .\} .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Sort::Key::Top 3pm" .TH Sort::Key::Top 3pm "2011-06-17" "perl v5.14.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Sort::Key::Top \- select and sort top n elements .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Sort::Key::Top (nkeytop top); \& \& # select 5 first numbers by absolute value: \& @top = nkeytop { abs $_ } 5 => 1, 2, 7, 5, 5, 1, 78, 0, \-2, \-8, 2; \& # ==> @top = (1, 2, 1, 0, \-2) \& \& # select 5 first numbers by absolute value and sort accordingly: \& @top = nkeytopsort { abs $_ } 5 => 1, 2, 7, 5, 5, 1, 78, 0, \-2, \-8, 2; \& # ==> @top = (0, 1, 1, 2, \-2) \& \& # select 5 first words by lexicographic order: \& @a = qw(cat fish bird leon penguin horse rat elephant squirrel dog); \& @top = top 5 => @a; \& # ==> @top = qw(cat fish bird elephant dog); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" The functions available from this module select the top n elements from a list using several common orderings and custom key extraction procedures. .PP They are all variations around .PP .Vb 1 \& keytopsort { CALC_KEY($_) } $n => @data; .Ve .PP In array context, this function calculates the ordering key for every element in \f(CW@data\fR using the expression inside the block. Then it selects and orders the \f(CW$n\fR elements with the lower keys when compared lexicographically. .PP It is equivalent to the pure Perl expression: .PP .Vb 1 \& (sort { CALC_KEY($a) cmp CALC_KEY($b) } @data)[0 .. $n\-1]; .Ve .PP If \f(CW$n\fR is negative, the last \f(CW$n\fR elements from the bottom are selected: .PP .Vb 2 \& topsort 3 => qw(foo doom me bar doz hello); \& # ==> (\*(Aqbar\*(Aq, \*(Aqdoz\*(Aq, \*(Aqdoom\*(Aq) \& \& topsort \-3 => qw(foo doom me bar doz hello); \& # ==> (\*(Aqfoo\*(Aq, \*(Aqhello\*(Aq, \*(Aqme\*(Aq) \& \& top 3 => qw(foo doom me bar doz hello); \& # ==> (\*(Aqdoom\*(Aq, \*(Aqbar\*(Aq, \*(Aqdoz\*(Aq) \& \& top \-3 => qw(foo doom me bar doz hello); \& # ==> (\*(Aqfoo\*(Aq, \*(Aqme\*(Aq, \*(Aqhello\*(Aq) .Ve .PP In scalar context, the value returned by the functions on this module is the cutoff value allowing to select nth element from the array. For instance: .PP .Vb 2 \& # n = 5; \& scalar(topsort 5 => @data) eq (sort @data)[4] # true \& \& # n = \-5; \& scalar(topsort \-5 => @data) eq (sort @data)[\-5] # true .Ve .PP Note that on scalar context, the \f(CW\*(C`sort\*(C'\fR variations (see below) are usually the right choice: .PP .Vb 1 \& scalar topsort 3 => qw(me foo doz doom me bar hello); # ==> \*(Aqdoz\*(Aq \& \& scalar top 3 => qw(me foo doz doom me bar hello); # ==> \*(Aqbar\*(Aq .Ve .PP Note also, that the index is 1\-based (it starts at one instead of at zero). The \f(CW\*(C`atpos\*(C'\fR set of functions explained below do the same and are 0\-based. .PP Variations allow to: .IP "\- use the own values as the ordering keys" 4 .IX Item "- use the own values as the ordering keys" .Vb 2 \& topsort 5 => qw(a b ab t uu g h aa aac); \& # ==> a aa aac ab b .Ve .IP "\- return the selected values in the original order" 4 .IX Item "- return the selected values in the original order" .Vb 2 \& top 5 => qw(a b ab t uu g h aa aac); \& # ==> a b ab aa aac .Ve .IP "\- use a different ordering" 4 .IX Item "- use a different ordering" For instance comparing the keys as numbers, using the locale configuration or in reverse order: .Sp .Vb 2 \& rnkeytop { length $_ } 3 => qw(a ab aa aac b t uu g h); \& # ==> ab aa aac \& \& rnkeytopsort { length $_ } 3 => qw(a ab aa aac b t uu g h); \& # ==> aac ab aa .Ve .Sp A prefix is used to indicate the required ordering: .RS 4 .IP "(no prefix)" 4 .IX Item "(no prefix)" lexicographical ascending order .IP "r" 4 .IX Item "r" lexicographical descending order .IP "l" 4 .IX Item "l" lexicographical ascending order obeying locale configuration .IP "r" 4 .IX Item "r" lexicographical descending order obeying locale configuration .IP "n" 4 .IX Item "n" numerical ascending order .IP "rn" 4 .IX Item "rn" numerical descending order .IP "i" 4 .IX Item "i" numerical ascending order but converting the keys to integers first .IP "ri" 4 .IX Item "ri" numerical descending order but converting the keys to integers first .IP "u" 4 .IX Item "u" numerical ascending order but converting the keys to unsigned integers first .IP "ru" 4 .IX Item "ru" numerical descending order but converting the keys to unsigned integers first .RE .RS 4 .RE .IP "\- select the head element from the list sorted" 4 .IX Item "- select the head element from the list sorted" .Vb 2 \& nhead 6, 7, 3, 8, 9, 9; \& # ==> 3 \& \& nkeyhead { length $_ } qw(a ab aa aac b t uu uiyii) \& # ==> \*(Aqa\*(Aq .Ve .IP "\- select the tail element from the list sorted" 4 .IX Item "- select the tail element from the list sorted" .Vb 2 \& tail qw(a ab aa aac b t uu uiyii); \& # ==> \*(Aquu\*(Aq \& \& nkeytail { length $_ } qw(a ab aa aac b t uu uiyii) \& # ==> \*(Aquiyii\*(Aq .Ve .IP "\- select the element at position n from the list sorted" 4 .IX Item "- select the element at position n from the list sorted" .Vb 2 \& atpos 3, qw(a ab aa aac b t uu uiyii); \& # ==> \*(Aqab\*(Aq; \& \& rnkeyatpos { abs $_ } 2 => \-0.3, 1.1, 4, 0.1, 0.9, \-2; \& # ==> 1.1 \& \& rnkeyatpos { abs $_ } \-2 => \-0.3, 1.1, 4, 0.1, 0.9, \-2; \& # ==> \-0.3 .Ve .Sp Note that for the atpos set of functions indexes start at zero. .IP "\- return a list composed by the elements with the first n ordered keys and then the remaining ones." 4 .IX Item "- return a list composed by the elements with the first n ordered keys and then the remaining ones." .Vb 2 \& ikeypart { length $_ } 3 => qw(a bbbb cc ddddd g fd); \& # ==> a cc g bbbb ddddd fd .Ve .IP "\- return two arrays references, the first array containing the elements with the first n ordered keys and the second with the rest." 4 .IX Item "- return two arrays references, the first array containing the elements with the first n ordered keys and the second with the rest." .Vb 2 \& keypartref { length $_ } 3 => qw(a bbbb cc ddddd g fd); \& # ==> [a cc g] [bbbb ddddd fd] .Ve .PP The full list of available functions is: .PP .Vb 1 \& top ltop ntop itop utop rtop rltop rntop ritop rutop \& \& keytop lkeytop nkeytop ikeytop ukeytop rkeytop rlkeytop rnkeytop \& rikeytop rukeytop \& \& topsort ltopsort ntopsort itopsort utopsort rtopsort rltopsort \& rntopsort ritopsort rutopsort \& \& keytopsort lkeytopsort nkeytopsort ikeytopsort ukeytopsort \& rkeytopsort rlkeytopsort rnkeytopsort rikeytopsort rukeytopsort \& \& head lhead nhead ihead uhead rhead rlhead rnhead rihead ruhead \& keyhead lkeyhead nkeyhead ikeyhead ukeyhead rkeyhead rlkeyhead \& rnkeyhead rikeyhead rukeyhead \& \& tail ltail ntail itail utail rtail rltail rntail ritail rutail \& keytail lkeytail nkeytail ikeytail ukeytail rkeytail rlkeytail \& rnkeytail rikeytail rukeytail \& \& atpos latpos natpos iatpos uatpos ratpos rlatpos rnatpos riatpos \& ruatpos keyatpos lkeyatpos nkeyatpos ikeyatpos ukeyatpos rkeyatpos \& rlkeyatpos rnkeyatpos rikeyatpos rukeyatpos \& \& part lpart npart ipart upart rpart rlpart rnpart ripart \& rupart \& \& keypart lkeypart nkeypart ikeypart ukeypart rkeypart \& rlkeypart rnkeypart rikeypart rukeypart .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" Sort::Key, \*(L"sort\*(R" in perlfunc. .PP The Wikipedia article about selection algorithms . .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright (C) 2006\-2008, 2011 by Salvador Fandin\*~o (sfandino@yahoo.com). .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.