Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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

by thundergnat (Deacon)
on Feb 05, 2013 at 19:58 UTC ( #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 (Pope) 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

      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:
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 ;~)

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1017267]
[stonecolddevin]: I think Stevie Ray is one of maybe 2 or 3 people that could take a Hendrix song and make you go "...huh. this is close." https://open. 0UVM9trirBK5s1xBwL oVhZ
[stonecolddevin]: https://open. 0UVM9trirBK5s1xBwL oVhZ rather
[Corion]: Sadly I must go to bed now :-| See y'all!
[erix]: I really loved/love this "Recorded Live" record, 1973 (yes, I'm that old)
[stonecolddevin]: goodnight Corion
[erix]: cya Corion :)
[stevieb]: Joan Jett, Hendrix, Neil Young, Dave Keuning (The Killers)... I have my mind elsewhere so I'm limited in my capacity to think about this righ tnow ;)
[erix]: another blues with Big Red :)
[stonecolddevin]: I will say John Mayer is an excellent guitarist, I don't like his music but he's pretty damn good
[erix]: scd: maybe you like John Mayall better :)

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2017-06-22 21:39 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (531 votes). Check out past polls.