in reply to Re^4: parsing output of UNIX `who` command (golf)
in thread parsing output of UNIX `who` command
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:
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 $_, $/ ^= '*'; } }
s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^6: parsing output of UNIX `who` command (golf)
by blahblah (Friar) on Oct 10, 2006 at 00:02 UTC | |
by chargrill (Parson) on Oct 10, 2006 at 00:59 UTC | |
by blahblah (Friar) on Oct 10, 2006 at 14:58 UTC | |
by shmem (Chancellor) on Oct 19, 2006 at 16:38 UTC |