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

Cool... And how about this one?

foreach (sub {pop;shift;@_}->(grep /start/../stop/, @data) ) { ... }

Sorry, just kidding :-)

UPDATE: taking Abigail and John M. Dlugosz's remarks into account, the original post wanted to stop after the first occurence of 'stop'. I guess we should then be using ?? instead of // to prevent matching more than once.

foreach (sub {pop;shift;@_}->(grep ?start?..?stop?, @data) ) { ... }

--bwana147

Replies are listed 'Best First'.
Re: flip-flop operator and sequence number
by Abigail (Deacon) on Jun 28, 2001 at 19:38 UTC
    That code doesn't do the same.
    #!/opt/perl/bin/perl -wl use strict; my @data = qw /foo start bar baz stop qux start quux stop fuzzle/; foreach (grep /start/ .. /stop/, @data) { next if /start/ || /stop/; print } print "----"; foreach (sub {pop; shift; @_} -> (grep /start/ .. /stop/, @data)) { print; } __END__ bar baz quux ---- bar baz stop start quux
    You are losing the boundaries of the flip-flop operator, and use the boundaries of grep. But those boundary events are not necessarely the same.

    -- Abigail

      That's a good point. But your code is not the same either, since his original said last not next. He was stopping after the first group and ignoring a subsequent /start/.
Re: Re: Re: flip-flop operator and sequence number
by John M. Dlugosz (Monsignor) on Jun 28, 2001 at 19:26 UTC
    I love it! Using the sub{}->() idiom you can modify a list in-place without using a temporary. Your code does the same as:
    my @temp= grep /start/../stop/, @data; pop @temp; shift @temp; foreach (@temp) { ... }
    right?

    —John

      Er, actually, I feel like a hobbit being caught red handed meddling in the affairs of wizards, which are, as everybody knows, subtle and quick to anger.

      I tried and fiddle a bit with this and it's somewhat interesting. @_ is not itself aliased with the list of arguments. But each element of @_ is individually aliased with each argument. That means that you can't change the list itself (i.e. the number of its elements), but you can the values of each element. If I do:

      sub { pop; shift; }->(@array);

      It'll leave @array unharmed. OTOH, if I do:

      sub { $_[0] = 'whatever' }->(@array);

      It will change the first element of @array. You can combine both (although I would not recommend it):

      sub { shift; $_[0] = 'whatever' }->(@array); # changes the *second* element of @array

      --bwana147

        Please meddle, Bilbo. I find it enlightening. A thread like this is much more interesting than just reading docs. So far I've learned:
        • Sequence Numbers: in the docs, but good refresher on how to get to them. Change loop to mid-decision test in scalar context via next/last.
        • Ending Number: will the E0 thing break in Perl 6, when it becomes 6 is true instead? Will the autotranslator handle this subtle point? Since the E0 is harmless, they should keep that for legacy, even if they add the property as well.
        • Your Sub Idiom: as noted in my node.
        • Put Real Code in Test: having a block with scratch work that returns the real result is something to keep in mind. Can't do that in C++.
        • Precidence
        • Aliasing

        —John