Range operator; flip, but prevent the flop and immediately click

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

outputs 3 4 5 6 7 8 9 10

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

Q2 : how to make the left part of the range operator unconditional true (i.e. true .. /7/ )?

UPDATE:

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

can be used as a replacement for true .. /7/ .

+6
source share
2 answers
  • Any false expression that is not constant inflection 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' 

    No trigger.

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

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

    No trigger.

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

    So, I changed 7 to 8 . To actually match 7 ,

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

    No trigger.

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

Q1

Use the *FAIL verb:

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

Which can be shortened to *F :

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

And for TIMTOWTDI :

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

Source: https://habr.com/ru/post/970318/


All Articles