Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: How do you move within an array using foreach?

by JaWi (Hermit)
on Oct 10, 2002 at 18:31 UTC ( #204267=note: print w/replies, xml ) Need Help??


in reply to How do you move within an array using foreach?

While not tested, I saw this elegant solution in this node:
print if /\[foo\]/ .. /\[foo2\]/ foreach ( @lines );
Credits go to zigdon... ;-)

Update: Check Ovid's answer; it's more strict in the requirements set.

-- JaWi

"A chicken is an egg's way of producing more eggs."

Replies are listed 'Best First'.
Re: Re: How do you move within an array using foreach?
by jbeninger (Monk) on Oct 10, 2002 at 18:51 UTC
    That looks like a useful thing to have around - I've never seen the /foo/ .. /bar/ notation before - I don't suppose you could explain it, or point me to a place that does?
      It's called the "flip-flop" operator. See perlop
      Look here in perlop.

      In scalar context, ".." returns a boolean value. The operator is bistable, like a flip-flop, and emulates the line-range (comma) operator of sed, awk, and various editors. Each ".." operator maintains its own boolean state. It is false as long as its left operand is false. Once the left operand is true, the range operator stays true until the right operand is true, AFTER which the range operator becomes false again.

      --

      flounder

      As a matter of fact, I can (since about 15 minutes :-)! Summarizing from perlop:

      The two dots work like a sort of flip-flop: if returns true if the first operation (the /foo/ part) evaluates `true' and stays true until the second operation evaluates to `true' (the /bar/ part). After that it returns false again.

      Cheerio,

      -- JaWi

      "A chicken is an egg's way of producing more eggs."

      Look in the perlop documentation, under the "Range Operators" section. All the various uses of .. are explained there in detail.

      -- Mike

      --
      just,my${.02}

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2022-08-13 23:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?