Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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 (Sage) 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://525406]
help
Chatterbox?
[Eily]: you could tie a variable into not having the same value each time, if you like to make people who try to debug your code facepalm
[Corion]: perl -wle 'package o; use overload q("") => sub {warn "str"; ""}, bool => sub{warn "bool"; 1}; package main; my $o={}; bless $o => o; print "Yay" if ($o && !length($o))'
[Corion]: But people writing such code should document the objects they construct and why it makes sense for an object to be invisible as string while being true in a boolean context
[hippo]: That's equal parts clever and horrendous.
[Eily]: the overload version wouldn't return true with "$x" && !length $x though, I guess
[hippo]: The more I look at this code, the more $x is a plain old scalar and the more this condition will never be true. I'm calling it a bug at this point.
[hippo]: Thanks for your input which has soothed my sanity (a little)
[Corion]: Eily: Sure - if you force both things into stringy things, then you break that magic. But that would also mean that you changed the expression, as now $x = 0.00 will be true instead of false as it were before
[Corion]: Ah no, at least in my feeble experiments that doesn't change the meaning
[Corion]: We sell sanity in small packages ;)

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2017-07-27 13:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (413 votes). Check out past polls.