Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
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 (Bishop) 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 cooling their heels in the Monastery: (5)
As of 2015-07-04 03:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls