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

mattr is right, merlyn's solution does not work.

But this modification does:

foreach ( grep { (/start/../stop/) !~ /^1?$|E/} @arry ){ #... }
I looked a bit further, and what is coming out of the (/start/../stop/) is the sequence number...until you put the not in front of it. Then it just yields blank or 1!

Any ideas why? Is it being evaluated in list context? Why does the not have any influence on the context?

-- iakobski

Replies are listed 'Best First'.
Re: Re: Re: flip-flop operator and sequence number
by merlyn (Sage) on Jun 28, 2001 at 19:29 UTC
    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

      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

Re: Re: Re: flip-flop operator and sequence number
by srawls (Friar) on Jun 28, 2001 at 19:27 UTC
    not puts things in a boolean context, either true or false. False is 0 and true is anything else (in numerical context at least), by convention we use 0 and 1 as false and true when we aren't concerned about data, just truthfulnes.

    The 15 year old, freshman programmer,
    Stephen Rawls

Re: Re: Re: flip-flop operator and sequence number
by John M. Dlugosz (Monsignor) on Jun 28, 2001 at 19:43 UTC
    That's a good observation on what not does.

    But, the binding operator =~ has a higher order of precidence, so the not applies to the result of the match, not to the left hand side being matched.

    —John