Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

help with this snippet: if($.==2||$&&&!$s++)

by gg48gg (Sexton)
on Nov 30, 2011 at 21:50 UTC ( #940945=perlquestion: print w/ replies, xml ) Need Help??
gg48gg has asked for the wisdom of the Perl Monks concerning the following question:

Need help understanding what the '$&&&!$s++' means in this code:

# svmon -Pt15 | perl -e 'while(<>){print if($.==2||$&&&!$s++);$.=0 if(/^-+$/)}'

I understand what the line of code does (filters output of svmon command on AIX), just not sure what that string is. Thanks

Comment on help with this snippet: if($.==2||$&&&!$s++)
Re: help with this snippet: if($.==2||$&&&!$s++)
by toolic (Chancellor) on Nov 30, 2011 at 22:00 UTC
Re: help with this snippet: if($.==2||$&&&!$s++)
by ikegami (Pope) on Nov 30, 2011 at 22:48 UTC

    « $&&&!$s++ » is « $& && !($s++) ».


    !$s++ is a neat construct. It's true the first time it is evaluated, and only the first time it is evaluated.

    $ perl -E'say !$s++ ? 1 : 0 for 1..5;' 1 0 0 0 0

    It's often used to remove duplicates.

    $ perl -E'say grep !$seen{$_}++, split //, "abracadabra";' abrcd

    As for $&, it is set by the match operator. The only match operator in the code is /^-+$/, so $& will be false until the first time that matches, and it will always be true afterwords.


    The code prints the following lines:

    • The second line of the file, unless the first line of the file consists entirely of dashes. [ I suspect this is unintentional and never happens. ]
    • The line that comes after the first line of the file consisting entirely of dashes.
    • The second line following a line consisting entirely of dashes, unless it or the line before it consists entirely of dashes.

    I'm surprise the person who wrote that doesn't know about -n. He also left in unneeded spaces and parens despite his golfing attempt.

    svmon -Pt15 | perl -e 'while(<>){print if($.==2||$&&&!$s++);$.=0 if(/^ +-+$/)}' | v svmon -Pt15 | perl -ne'print if$.==2||$&&&!$s++;$.=0if/^-+$/' svmon -Pt15 | perl -nlE'say if$.==2||$&&&!$s++;$.=0if/^-+$/'
Re: help with this snippet: if($.==2||$&&&!$s++)
by DrHyde (Prior) on Dec 01, 2011 at 10:30 UTC

    It means that the author thought that a superficial appearance of cleverness was more important than doing his job properly - that is, writing stuff that is easily understood.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://940945]
Approved by toolic
Front-paged by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2014-08-28 04:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (256 votes), past polls