简体   繁体   中英

Range operator; flip but prevent flop, and make immediate flip

perl -wle 'print join " ", grep /3/ .. undef(), 1..10'

outputs 3 4 5 6 7 8 9 10

Q1 : Is there better way than undef to prevent flop?

Q2 : How to force left part of range operator to unconditional true (ie. true .. /7/ )?

UPDATE:

perl -wE 'say join " ", grep { ((/7/ .. undef)||1) ==1 } 1..10'

could be used as true .. /7/ replacement.

  1. Any false expression that isn't constant-folded to a number will do.

     perl -wE'say join " ", grep $_==3 .. undef, 1..10' perl -wE'say join " ", grep $_==3 .. do{0}, 1..10' perl -wE'say our $FALSE; say join " ", grep $_==3 .. $FALSE, 1..10' 

    Without a flip-flop.

     perl -wE'my $ok; say join " ", grep $ok ||= $_==3, 1..10' 
  2. If you want the boolean opposite of something, use negation!

     perl -wE'say join " ", grep !($_==8 .. undef), 1..10' 

    Without a flip-flop.

     perl -wE'my $done; say join " ", grep !($done ||= $_==8), 1..10' 

    Ok, so I changed 7 to 8 . To actually match on 7 ,

     perl -wE'my $last; say join " ", grep { my $x = ($_==7 .. undef); !$x || $x == 1 } 1..10' 

    Without a flip-flop.

     perl -wE'my $done; say join " ", grep { my $rv = $done; $done ||= $_==7; !$rv } 1..10' 

Q1

Use the *FAIL verb:

print join " ", grep /3/ .. /(*FAIL)/, 1 .. 10;

Which can be abbreviated to just *F :

print join " ", grep /3/ .. /(*F)/, 1 .. 10;

And for the TIMTOWTDI :

print join " ", grep /3/ .. /(?!)/, 1 .. 10;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM