Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Flipin good, or a total flop?

by ysth (Canon)
on Jan 25, 2006 at 11:06 UTC ( #525406=note: print w/replies, xml ) Need Help??

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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://525406]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2023-02-05 15:03 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (32 votes). Check out past polls.