Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://940963]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2018-01-24 09:34 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (256 votes). Check out past polls.