unixguy has asked for the wisdom of the Perl Monks concerning the following question:

Hi, This perl one liner will print top ten memory using process. can you explain how this perl oneliner works?
svmon -Pt15 | perl -e 'while(<>){print if($.==2||$&&&!$s++); $.=0 if(/^-+$/)}'
thanks

Replies are listed 'Best First'.
Re: can you explain this perl oneliner
by Utilitarian (Vicar) on Jan 23, 2010 at 02:24 UTC
    OK, so you're using AIX, not everyone is familiar with it, so consider providing sample input to the Perl script
    svmon -P, mem usage for the following PIDs -t$n, Displays memory usage statistics for the top $n processes
    So that's our input. We skip the first line by insisting that $. (input line number) is 2
    Then allow following lines with the OR $& (last pattern match, from line 2 on exists)
    There is also a redundant check on !$s++ which only can return true when $s=0
    however if the line consists of dashes "-" reset the counter to zero.

    For more precise answers try adding the input, the result of svmon -Pt15 to the enquiry.
    Hope this helps.
    UPDATEBy the way this code is horribly inefficient and seems to be written for obscurity more than for effectiveness

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: can you explain this perl oneliner
by Anonymous Monk on Jan 23, 2010 at 02:16 UTC
    $ perl -MO=Deparse -e "while(<>){print if($.==2||$&&&!$s++);$.=0 if(/^ +-+$/)}" while (defined($_ = <ARGV>)) { print $_ if $. == 2 or $& and not $s++; $. = 0 if /^-+$/; } -e syntax OK
    It prints the 2rd line read, and 2nd line after every line of ----. Also, it prints the first line after the first line of ----.