Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Perl Sorting One Liner

by Siccula (Initiate)
on May 06, 2013 at 19:33 UTC ( #1032388=perlquestion: print w/ replies, xml ) Need Help??
Siccula has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl monks, I have a problem. I a reading data from a file using a perl one-liner like so.

perl -MList::Util=max -F'\n' -0777naE'$w=max map length,@F;say pack"(A +$w)9 A*",splice @F,0,10 while @F;' Data.txt

This prints data from the file in columns, but now I need to find a way to sort a certain column(lets say alphabetically). I am knew to perl, and I am not sure where to begin. Is there a way to do this using a "one-liner", if not may you please help me develop a way to do this. Thank you very much!

Comment on Perl Sorting One Liner
Download Code
Re: Perl Sorting One Liner
by Anonymous Monk on May 06, 2013 at 19:39 UTC

    If a one liner needs another feature, it is time to save it into a normal program on disk, break it up into multiple lines and comment it.

      I cannot agree more! Few of us have the typing skill to benefit from a one-liner as long as the current one.
      Bill
Re: Perl Sorting One Liner
by Anonymous Monk on May 06, 2013 at 19:52 UTC
      What part of this boilerplate response is relevant to the OP's question?
      Just another Perler interested in Algol Programming.

        What part of this boilerplate response is relevant to the OP's question?

        Every single part :)
        a place to begin
        figuring out what the oneliner does
        turning the oneliner into a multiliner
        a place to begin when you're stuck

Re: Perl Sorting One Liner
by kcott (Abbot) on May 07, 2013 at 11:12 UTC

    G'day Siccula,

    Welcome to the monastery.

    Here's an example showing various techniques used in sorting columns:

    $ perl -Mstrict -Mwarnings -E ' use constant { FIRST => 0, SECOND => 1, THIRD => 2 }; my @data = qw{ A:C:1 A:C:2 A:D:1 A:D:2 B:C:1 B:C:2 B:D:1 B:D:2 }; say "Raw data:"; say for @data; say "Sorted data:"; say join ":" => @$_ for sort { $b->[FIRST] cmp $a->[FIRST] || $a->[SECOND] cmp $b->[SECOND] || $b->[THIRD] <=> $a->[THIRD] } map { [ split /:/ ] } @data; ' Raw data: A:C:1 A:C:2 A:D:1 A:D:2 B:C:1 B:C:2 B:D:1 B:D:2 Sorted data: B:C:2 B:C:1 B:D:2 B:D:1 A:C:2 A:C:1 A:D:2 A:D:1

    Here, FIRST column is sorted into descending alphabetical order; where items are the same, SECOND column is sorted into ascending alphabetical order; where items are the same, THIRD column is sorted into descending numerical order.

    References: sort; perlvar (for $a and $b); perlop (for cmp and <=>).

    Decide whether sorting will be applied to more than one column and, if so, what order to use. For instance, to sort by last name and then, if last names are the same, sort by first name, you might use something like:

    $a->[LAST_NAME] cmp $b->[LAST_NAME] || $a->[FIRST_NAME] cmp $b->[FIRST +_NAME]

    Look at the type of data you're sorting and use the appropriate operator: cmp for strings and <=> for numbers.

    For ascending sorts (0 -> 9, A -> Z, etc.) compare $a data to $b data (e.g. $a->[SECOND] cmp $b->[SECOND]); for descending sorts, reverse the $a and $b values (e.g. $b->[THIRD] <=> $a->[THIRD]).

    I've implemented the example as a one-liner (albeit with some formatting for readability purposes). So, for a one-off, throw-away command line, a one-liner is fine; however, save the code to a script if you're ever going to: run this again; need a record of what code you used; want to refer back to it to refresh your memory on how to do this.

    -- Ken

      This response is not just irrelevant; it is a near-farcical example of what is wrong with Perlmonks. What does the "strict and warnings" chant buy you in a one-liner? Does defining a constant FIRST as 0 improve the code? Why give an example using delimited fields when the OP is clearly using fixed-width ones?

      You parrot some random tropes that will hopefully yield upvotes, but do nothing to answer the question.

      Just another Perler interested in Algol Programming.
        Hurry up educated_foo, a few more helpful posts like this and you'll break 700 writeups by august

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1032388]
Approved by graff
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (11)
As of 2014-08-28 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (259 votes), past polls