Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

by ikegami (Pope)
on Nov 30, 2011 at 22:48 UTC ( #940963=note: print w/ replies, xml ) Need Help??


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

« $&&&!$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/^-+$/'


Comment on Re: help with this snippet: if($.==2||$&&&!$s++)
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (12)
As of 2015-07-03 18:44 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 (55 votes), past polls