Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Golf: reverse sort /etc/passwd by UID

by choroba (Cardinal)
on Feb 06, 2013 at 15:29 UTC ( [id://1017456]=note: print w/replies, xml ) Need Help??


in reply to Golf: reverse sort /etc/passwd by UID

Only suitable for a terminal, not to be redirected to a file:
# 1 2 3 4 # 12345678901234567890123456789012345678901234 perl -e 'print sort map 9/(1+(split/:/)[2])."\r$_",<>' /etc/passwd
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Golf: reverse sort /etc/passwd by UID
by tobyink (Canon) on Feb 06, 2013 at 22:11 UTC

    Nice, but one of the lines in my /etc/passwd file comes out as:

    1.52590218966964e-05\rnobody:x:65534:65534:nobody:/nonexistent:/bin/sh

    And thus gets sorted after root.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      OK, just change 1/ to 9/ (or 8 or 7). Updated.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^2: Golf: reverse sort /etc/passwd by UID
by MidLifeXis (Monsignor) on Feb 06, 2013 at 18:02 UTC

    Cool (and sneaky)!

    Nice use of the features of how floats are printed and the behavior of a console. Could this run into problems with a very short passwd line?

    1. Takes the third field
    2. Adds one to avoid div by 0
    3. Inverts it (1/X) to get the sort order correct
    4. appends a carriage return (beginning of line)
    5. appends the original line
    6. sorts numerically lexically, but with all values being less than 1 it does not make a difference.
    7. prints results

    Update: Updated how sort happens.

    Update 2: s/<ul>/<ol>/

    --MidLifeXis

      I'm not clear on how it sorts on a field in the text while at the same time appending the text to the field which is sorted. What am I missing here?

      Tommy
      A mistake can be valuable or costly, depending on how faithfully you pursue correction
        at the same time
        Nothing is happening at the same time. The steps shown by MidLifeXis are ordered (maybe <ol> would have been better than <ul>). First, the text is concatenated, then it is sorted.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^2: Golf: reverse sort /etc/passwd by UID
by Tommy (Chaplain) on Feb 06, 2013 at 17:24 UTC

    Good gosh, can't even decipher that. Sorting the output of a map applying a mathematical expression to the list of lines on STDIN... but how do you discard the text it generated here --> (1+(split/:/)[2])<-- ??

    Tommy
    A mistake can be valuable or costly, depending on how faithfully you pursue correction

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1017456]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-25 06:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found