Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
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 exploiting the Monastery: (8)
As of 2014-12-26 04:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (165 votes), past polls