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

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

by thundergnat (Deacon)
on Feb 05, 2013 at 19:58 UTC ( [id://1017267]=note: print w/replies, xml ) Need Help??


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

perl -E"say reverse sort{/(:\d+:)/<=>/(:\d+:)/}<>" /etc/passwd

It's quite possible that I'm being exceedingly dense, but how is this any different from just:

perl -E"say reverse<>" /etc/passwd

? Perhaps you meant

perl -E"say sort{($b=~/:(\d+)/)[0]<=>($a=~/:(\d+)/)[0]}<>" /etc/passwd

Replies are listed 'Best First'.
Re^3: Golf: reverse sort /etc/passwd by UID
by BrowserUk (Patriarch) on Feb 05, 2013 at 20:11 UTC

    D'oh! I did omit a couple of bits didn't I (It was (obviously) untested -- I don't have an /etc/passwd).

    I tried to remove the $b=~ & $a=~ bits when I noticed that reverse was one character less -- ignoring/forgetting that $_ cannot be both at the same time :(.

    I don't think the (...)[0] bits are necessary, without /g only the regex will only return one (the first) match.

    This should work? (still untested):

    perl -E"say sort{$b=~/:(\d+):/<=>$a=~/:(\d+):/}<>" /etc/passwd

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      No, unfortunately that doesn't work either. A regex match in scalar context returns 1 or 0. It needs to be called in list context to return the value.
      >perl -E"print 'foo'=~/(o+)/;" oo
      >perl -E"print scalar 'foo'=~/(o+)/;" 1

      No, no. It does work (tested and confirmed). See my reply to thundergnat.

      NOTE- remember you have to omit the typos though, as I already pointed out. So far, the winner remains:

      UPDATE: thundergnat is right that the code below DOESN'T work as intended

      perl -e 'print reverse sort{/(:\d+:)/<=>/(:\d+:)/}<>' /etc/passwd

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

        No, no. It doesn't work (tested and confirmed).

        The sort block operates on the global variables $a and $b. A match without an explicit variable operates on $_. There is nothing mapping $a and $b to $_ there, so the sort block is just comparing 0 to 0 and leaving the order alone. The fact that /etc/password is in a "sorted" order makes it SEEM like it is working... but it isn't.

        print reverse sort{/(:\d+:)/<=>/(:\d+:)/}<DATA>; __DATA__ aaa:5: bbb:3: ccc:1: ddd:7: eee:8:
        eee:8:
        ddd:7:
        ccc:1:
        bbb:3:
        aaa:5:
        
Re^3: Golf: reverse sort /etc/passwd by UID
by Tommy (Chaplain) on Feb 05, 2013 at 20:21 UTC
    It's quite possible that I'm being exceedingly dense, but how is this any different from just:
    perl -E"say reverse<>" /etc/passwd

    UPDATE- we only wanted it to work as described below-- with implicit reassignment of $_ to $a and also to $b.

    It's not the same. BrowserUK's solution uses two regexes, each with a capture group. The capture groups implicitly assign to what would be the equivalents of "$a" and "$b". Since the point of this excercise was to save typing (potentially at the loss of clarity), BrowserUK didn't bother wasting precious keystrokes in explicitly assigning the result of the regex capture groups to an "$a" or "$b". Instead, he/she (I don't know BrowserUK personally) just let Perl handle those details. BrowserUK definitely meant what was posted ;~)

    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://1017267]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-25 11:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found