Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^3: Foreach Loops

by Roy Johnson (Monsignor)
on Mar 15, 2005 at 22:41 UTC ( #439799=note: print w/replies, xml ) Need Help??

in reply to Re^2: Foreach Loops
in thread Foreach Loops

I'm so glad you asked!

I am using a flip-flop as a counter. Of the conditions I gave it, the left side is always true and the right is always false, so the expression is always true. It is a property of the operator that it returns a count of the number of consecutive times it has been true. So subtracting one from that gives you the index.

Unless there are redos or nexts, which would throw it off. So I really should have written

my $index = 0; for my $line (@arr) { next if $index % 4 == 0; print "$index = $line\n"; } continue { $index = (1=~//..1=~/0/); }
Well, actually, I shouldn't have written it at all as a solution to this question. But I like putting little-used operators into the spotlight so that people might think of them when they actually are appropriate.

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^4: Foreach Loops
by tbone1 (Monsignor) on Mar 16, 2005 at 13:03 UTC

    Pardon me, Roy, if I thank you; that is absolutely brilliant. (Include Guinness ad here.) I've never seen that before, and I wouldn't have thought of it in a month of Sundays. I have no idea where I would use that in place of normal indexing, but I might have to go out of my way to use this just to keep my coworkers from thinking that Perl is simple. Nothing like some obscurata for enhancing job security.

    tbone1, YAPS (Yet Another Perl Schlub)
    And remember, if he succeeds, so what.
    - Chick McGee

      I'm glad you got a kick out it, but I really have to discourage you from using it for indexing. Can you imagine explaining it?
      "What the hell is this mess?"
      "Oh, I'm tracking the index of the element of the array."
      "That's how you get an array index in Perl?! That's ridiculous. We're switching everything to Visual Basic®!"
      The most common use of a flip-flop is to extract a range of lines from a file. You set the left side to match the start of the range, and the right side to match the end of the range. If either operand is a constant expression, it is implicitly compared to $. (the line number). That is what kept me from using a simple expression like (1==1)..(1==0). Actually, now that I think about it, the expressions I used ought to be constant expressions.

      Anyway, I did think of a couple interesting alternatives for always-true flip-flops.

      $index = (!$index)...(!$index); #or $index = ($.||1)..($.&&0);
      Note that in the former, I have to use the 3-dot version, so that both operands aren't evaluated on the same pass. $index starts out zero, so the left side is true. After that, it's non-zero, so the right side is false.

      The second example just incorporates a variable into an expression whose truth is constant. I used $. because of its association with the operator, but any variable would do.

      Caution: Contents may have been coded under pressure.
Re^4: Foreach Loops
by Anonymous Monk on Mar 16, 2005 at 12:44 UTC
    Jeezus! "little-used" would be the exaggeration of the day here!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://439799]
[vedagiri89]: how to fix: Attempt to reload aborted
[hippo]: Ah, presumably 7 then.
[vedagiri89]: CentOS Linux release 7.0.1406 (Core)
[hippo]: How did you install libapreq2?
[vedagiri89]: that one is done by system team from the tar i guess
[hippo]: Really? Not using the one from the CentOS repository? Perhaps that's the problem.
[vedagiri89]: i need when this issue will come usually
[hippo]: How about mod_perl? Was that installed from the CentOS packge or from source?
[vedagiri89]: from the source
[hippo]: But is under vendor_perl in your error message so you are mixing up modules installed from the packager and by hand. That's rarely a good plan.

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2018-06-19 11:35 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (113 votes). Check out past polls.