in reply to Re: Re: flip-flop operator and sequence number
in thread flip-flop operator and sequence number

Uh, it is working. The start match is /1/, which also triggers on "10". So the output is:
1 start - don't show 2 in - show 3 end - don't show 4 out 5 out 6 out 7 out 8 out 9 out 10 start - don't show 11 in - show
It's exactly right. My solution works fine, no need to modify it.

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: Re: Re: Re: flip-flop operator and sequence number
by danger (Priest) on Jun 28, 2001 at 20:20 UTC

    What merlyn says is true enough, although there may be some confusion depending on what version of Perl you run merlyn's example with:

    [danger:jandrew:~]$ cat blah.pl #!/usr/bin/perl -w use strict; my @data = qw/one start two three stop four/; foreach (grep {not (/^start/../^stop/) =~ /^1?$|E/ } @data) { print "o> $_\n"; } [danger:jandrew:~]$ perl5.00503 blah.pl o> two o> three [danger:jandrew:~]$ perl5.6.1 blah.pl o> one o> start o> two o> three o> stop o> four

    Because, in 5.6+, the not operator with parens acts like a function (returns the negation of what's in the parens), so you'd want another set of parens to properly delimit what we want to negate (or use a unary +):

    my @data = qw/one start two three stop four/; foreach (grep {not ((/^start/../^stop/) =~ /^1?$|E/) } @data) { print "o> $_\n"; } # or: my @data = qw/one start two three stop four/; foreach (grep {not +(/^start/../^stop/) =~ /^1?$|E/ } @data) { print "o> $_\n"; }
      Thanks danger, I understand now. I have been wondering why the two gave such different results when they look so similar. I guess I'd better read the 5.6 docs again - I was assuming that the low precedence of not would make sure that the regex operation completed first - which as you have pointed out is true for versions prior to 5.6

      Apologies to merlyn for saying that he was wrong, but I did test his example, and I did try to understand what was going on. I wasn't rude, so I am a bit upset at being --'d for my node.

      I have learnt a lot in this node and merlyn's code, with danger's modification is going into the production system today.

      Thanks monks.

      -- iakobski