in reply to One-liners: Conditionals in Loops

I used to believe that Perl 6 will have this feature. I know that Ruby does already. I suspect that it would be complex to add to Perl 5 given how complex the parsing logic is already.

What I don't like about the feature is that when the logic gets more complex, you need to completely take the loop apart and rewrite. The more complex you let it get before that rewrite happens, the more painful the rewrite is. But if you want to keep it as a one-liner now, then I suggest that you write it like this:

print "$_\n" for grep /pattern/, @arr;
(I find that clearer than using boolean operators. Yes, they work, but I have to think more about what the author is trying to do...YMMV though.)

UPDATE: I realized that I was unclear about something. Ruby does not have an inline form of the basic for loop like it does if, unless, while and until. But the ones that it does do inline it allows you to stack as many deep as you want.

UPDATE 2: My beliefs have changed in view of TheDamian's response...

Replies are listed 'Best First'.
Re: Re: One-liners: Conditionals in Loops
by TheDamian (Vicar) on Nov 05, 2003 at 04:12 UTC
    I believe that Perl 6 will have this feature.
    I'm not so sure. Every time the topic has been raised, Larry has remained steadfastly adamant that stackable qualifiers won't be added to Perl 6.

    Of course in Perl 5 you can already do this:

    # Perl 5 do{print "$_\n" if /pattern/} for @arr;
    at the cost of four extra characters. And that would probably be reduced to two extra characters in Perl 6, given the new "every-block-is-a-closure" semantics:
    # Perl 6 {print "$_\n" if /pattern/} for @arr;