http://qs321.pair.com?node_id=525406


in reply to Flipin good, or a total flop?

A common thing with flipflop is to want to exclude one or both endpoints. To do this, you need to actually check the scalar value returned by the .. operator; it will be a number beginning at 1 when the flip condition is met and increasing once each time thereafter, with an "E0" appended when the flop condition is met. (False is returned as "".)

Anyway, here are some examples. Better suggestions greatly encouraged.

$ cat data initial start interior end final $ # Include both endpoints $ perl -wlne'print if /start/../end/' data start interior end $ # Exclude starting point $ perl -wlne'print if ((/start/../end/) || 0) > 1' data interior end $ # Regex alternative for exclude starting point $ perl -wlne'print if (/start/../end/) =~ /^(?!1(?!\d))\d/' data interior end $ # Exclude ending point $ perl -wlne'print if (/start/../end/) =~ /^\d+$/' data start interior $ # Exclude both endpoints $ perl -wlne'print if (/start/../end/) =~ /^\d+(?<!^1)$/' data interior $ # or: $ perl -wlne'print if (/start/../end/) !~ /^1?$|E/' data interior

Replies are listed 'Best First'.
Re^2: Flipin good, or a total flop?
by bart (Canon) on Jan 25, 2006 at 22:25 UTC
    If you're really interested on whether the start condition and/or end condition were triggered, there's another way: using variables to capture the test results. Like this:
    if((my $start = /start/) .. (my $end = /end/)) { ...
    Later, you simply have to test the boolean value of $start and $end:
    print "First time\n" if $start; print "Last time\n" if $end;
    but only in the if BLOCK, of course (lexical scope for the variables).
      I'm pretty sure that works the same way
      my $i = 13 if $expr;
      does, and is not to be trusted to continue to do what you mean.

      No, maybe I'm wrong. Hmm.

      Update: as long as you don't modify $start or $end except in cases where they were already set, I think you can count on this continuing to work, even if the my $foo if bar thing is "fixed". But I'd expect that if a warning is added for the my $foo if bar thing, that you'd get the warning with your code also.

        bar() if my $foo;
        is different from
        my $foo if bar(); # don't do this!

        The first case is equivalent to

        if (my $foo) { # $foo is lexically scoped to this block bar(); }
        just like how
        for my $foo (@foo) { # $foo is lexically scoped to this block baz(); }

        So if ((my $start =~ /start/) .. (my $end =~ /end/)) is perfectly happy.

Re^2: Flipin good, or a total flop?
by GrandFather (Saint) on Jan 25, 2006 at 11:19 UTC

    Thank you. I intended to mention that and forgot about it :(

    Note that selected itterations, not just the start and end points, can be handled as special cases using the FF return value.


    DWIM is Perl's answer to Gödel