.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" 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" '' . ds C` . ds C' '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 >0, 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. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Sort::Fields 3pm" .TH Sort::Fields 3pm "2022-10-13" "perl v5.34.0" "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::Fields \- Sort lines containing delimited fields .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 3 \& use Sort::Fields; \& @sorted = fieldsort [3, \*(Aq2n\*(Aq], @lines; \& @sorted = fieldsort \*(Aq\e+\*(Aq, [\-1, \-3, 0], @lines; \& \& $sort_3_2n = make_fieldsort [3, \*(Aq2n\*(Aq], @lines; \& @sorted = $sort_3_2n\->(@lines); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Sort::Fields provides a general purpose technique for efficiently sorting lists of lines that contain data separated into fields. .PP Sort::Fields automatically imports two subroutines, \f(CW\*(C`fieldsort\*(C'\fR and \&\f(CW\*(C`make_fieldsort\*(C'\fR, and two variants, \f(CW\*(C`stable_fieldsort\*(C'\fR and \&\f(CW\*(C`make_stable_fieldsort\*(C'\fR. \f(CW\*(C`make_fieldsort\*(C'\fR generates a sorting subroutine and returns a reference to it. \f(CW\*(C`fieldsort\*(C'\fR is a wrapper for the \f(CW\*(C`make_fieldsort\*(C'\fR subroutine. .PP The first argument to make_fieldsort is a delimiter string, which is used as a regular expression argument for a \f(CW\*(C`split\*(C'\fR operator. The delimiter string is optional. If it is not supplied, make_fieldsort splits each line using \f(CW\*(C`/\es+/\*(C'\fR. .PP 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 \*(L"1\*(R" indicates the first field, \*(L"2\*(R" indicates the second, and so on. A negative specifier like \*(L"\-2\*(R" means to sort on the second field in reverse (descending) order. To indicate a numeric rather than alphabetic comparison, append \*(L"n\*(R" to the specifier. A specifier of \*(L"0\*(R" means the entire string (\*(L"\-0\*(R" means the entire string, in reverse order). .PP 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. .PP \&\f(CW\*(C`fieldsort [1, 2], @data\*(C'\fR is roughly equivalent to \&\f(CW\*(C`make_fieldsort([1, 2])\->(@data)\*(C'\fR. 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 \f(CW\*(C`make_fieldsort\*(C'\fR once and reuse the subroutine it returns. .PP \&\f(CW\*(C`stable_fieldsort\*(C'\fR and \f(CW\*(C`make_stable_fieldsort\*(C'\fR are like their \&\*(L"unstable\*(R" counterparts, except that the items that compare the same are maintained in their original order. .SH "EXAMPLES" .IX Header "EXAMPLES" Some sample data (in array \f(CW@data\fR): .PP .Vb 8 \& 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 [\*(Aq1n\*(Aq], @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 [\*(Aq\-1n\*(Aq], @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, \*(Aq1n\*(Aq, \*(Aq\-3n\*(Aq], @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 \*(Aq(?:\e.|\es+)\*(Aq, [\*(Aq4n\*(Aq, \*(Aq3n\*(Aq], @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, \*(Aq0n\*(Aq], @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 .Ve .SH "BUGS" .IX Header "BUGS" Some rudimentary tests now. .PP Perhaps something should be done to catch things like: .PP .Vb 1 \& fieldsort \*(Aq.\*(Aq, [1, 2], @lines; .Ve .PP \&\f(CW\*(Aq.\*(Aq\fR translates to \f(CW\*(C`split /./\*(C'\fR \*(-- probably not what you want. .PP Passing blank lines and/or lines containing the wrong kind of data (alphas instead of numbers) can result in copious warning messages under \f(CW\*(C`\-w\*(C'\fR. .PP If the regexp contains memory parentheses (\f(CW\*(C`(...)\*(C'\fR rather than \f(CW\*(C`(?:...)\*(C'\fR), split will function in \*(L"delimiter retention\*(R" 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. .PP Not really a bug, but if you are planning to sort a large text file, consider using \fBsort\fR\|(1). Unless, of course, your operating system doesn't have \fBsort\fR\|(1). .SH "AUTHOR" .IX Header "AUTHOR" Joseph N. Hall, \f(CW\*(C`\*(C'\fR .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fBperl\fR\|(1).