Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^4: parsing output of UNIX `who` command (golf)

by blahblah (Friar)
on Oct 09, 2006 at 22:02 UTC ( [id://577276]=note: print w/replies, xml ) Need Help??


in reply to Re^3: parsing output of UNIX `who` command (golf)
in thread parsing output of UNIX `who` command

I downloaded this when it was posted and have been poking my brain at it for a little while. Now my brain hurts.
Any chance you could break it down for us obfunoobs?

  • Comment on Re^4: parsing output of UNIX `who` command (golf)

Replies are listed 'Best First'.
Re^5: parsing output of UNIX `who` command (golf)
by chargrill (Parson) on Oct 09, 2006 at 22:39 UTC

    Updated: Added in the forgotten opening brace before the print.

    The parent of your node seems to work, while the first response to it doesn't work the same way. I'll break down the one that works :)

    perldoc perlrun says:

    -n causes Perl to assume the following loop around your program, which makes it iterate over filename argu- ments somewhat like sed -n or awk: LINE: while (<>) { ... # your program goes here }

    So, with the golfed version: who|perl -ne'/ /,$;{$`}++}{print$_,$/^="*"for%', we can take out what was inside the single quotes and put that together manually:

    while(<>){ / /, $;{$`}++}{print $_, $/ ^= "*" for % }

    The interesting thing to notice is now this code isn't properly indented, so let's fix that:

    while(<>){ / /, $;{$`}++ } { print $_, $/ ^= "*" for % }

    What I didn't realize is that apparently -n also apparently adds (?) terminal semicolons (someone correct me if I'm wrong, but with what's happening here certainly seems to be the case), so now it's better re-written as:

    while(<>){ / /, $;{$`}++ } { print $_, $/ ^= "*" for %; }

    Starting to get clearer. Let's just make it crystal clear:

    use English; while( <ARGV> ){ m/\s/; $hash{$PREMATCH}++; } { for( %hash ){ print $_, $INPUT_RECORD_SEPARATOR ^= '*'; } }

    All that's left to explain is that:

    • $PREMATCH contains the username
    • $hash{'username'} counts how many times a user is logged in
    • $INPUT_RECORD_SEPARATOR (by default, a newline) when XOR'd with an asterisk becomes a space, such that the first time through the loop (on the first hash key), "\n" ^ "*" is a space, and now that's stored back in $INPUT_RECORD_SEPARATOR. Next time through the loop (the first hash value), a space XOR'd with an asterisk is a newline, and stored back in $INPUT_RECORD_SEPARATOR.

    Hence the output:

    $username $num_logins $username $num_logins

    Running it through B::Deparse confirms this:

    $ who|perl -MO=Deparse -ne'/ /,$;{$`}++}{print$_,$/^="*"for%' LINE: while (defined($_ = <ARGV>)) { / /, ++$;{$`}; } { foreach $_ (%;) { print $_, $/ ^= '*'; } }


    --chargrill
    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
      thats the one I was interested in, thanks! The only part that still has me wondering is the extra hanging curly brace:
      while(<>){ / /, $;{$`}++ } print $_, $/ ^= "*" for % } # <--- this hanging bit
      with no opening curly brace I don't understand why its there. Its even more odd in the golf:
      who|perl -ne'/ /,$;{$`}++}{print$_,$/^="*"for%' ^^^^ what?
      in any event, thank you very much for the very excellent explanation. I have to go read more about XOR now.... ;) blahblah

        Whoops, I dropped the opening brace from my de-golf'ing. I'll correct that above, but also try to explain it a little more explicitly.

        The thing to remember is that the perldoc perlrun entry for -n is applied literally.

        Again from perldoc:

        while (<>) { ... # your program goes here }

        So -n literally puts that code around yours.

        while(<>){ / /, $;{$`}++}{print $_, $/ ^= "*" for % # this line is what # was inside the single qu +otes }

        Literally. The }{ closes the while loop and lets you add extra code outside the while loop. And since -n will always add that extra closing brace, you need to open one. Resulting in:

        while(<>){ / /, $;{$`}++ } { print $_, $/ ^= "*" for % }


        --chargrill
        s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-26 08:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found