Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

One-liners: Conditionals in Loops

by David Caughell (Monk)
on Nov 04, 2003 at 20:45 UTC ( #304531=perlquestion: print w/ replies, xml ) Need Help??
David Caughell has asked for the wisdom of the Perl Monks concerning the following question:

I know that Perl has a miriad of shortcuts, and this is one of the reasons that I really like the language.

However, something that I think would be pretty cool, is if it was possible to write a conditional statement within a loop in a one-liner.

I haven't checked out the source code of perl, so I don't know how complex it would be to add something like this to the language (or how much slower a script would run).

#how it currently works for (@arr) { print "$_\n" if /pattern/; } #what I'm suggesting print "$_\n" if /pattern/ for @arr;

Are there technical difficulties implementing something of this kind?

Are there reasons that doing something like this would be bad programming?

Thanks,
Dave.

$scratchpad_public = 0 unless $scratchpad;

Comment on One-liners: Conditionals in Loops
Download Code
Re: One-liners: Conditionals in Loops
by particle (Vicar) on Nov 04, 2003 at 20:53 UTC

    to solve this specific problem, use perl -le"/pattern/ && print for @arr". this change handles the logic found in most (of my) one-liners.

    ~Particle *accelerates*

Re: One-liners: Conditionals in Loops
by Enlil (Parson) on Nov 04, 2003 at 20:58 UTC
    There is also grep (for conditionals):
    perl -le 'print $_ for grep{/pattern/}@arr'

    -enlil

Re: One-liners: Conditionals in Loops
by Zaxo (Archbishop) on Nov 04, 2003 at 21:12 UTC

    The trinary operator is another possibility. It allows an if.. else.. construction and can be nested. print /pattern/ ? "$_$/" : '' for @arr;

    After Compline,
    Zaxo

Re: One-liners: Conditionals in Loops
by delirium (Chaplain) on Nov 04, 2003 at 21:12 UTC
    print /(.*pattern.*)/s for @arr

    This would work if @arr had newlines.

Re: One-liners: Conditionals in Loops
by tilly (Archbishop) on Nov 04, 2003 at 21:13 UTC
    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...

      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;
•Re: One-liners: Conditionals in Loops
by merlyn (Sage) on Nov 04, 2003 at 21:27 UTC
    The trailing modifiers (putting if/while/unless/until/foreach after an expression) come from BASIC-PLUS, which Larry and I both hacked earlier in our respective lives.

    In BASIC-PLUS, you could nest them, and the result was always tough to parse as things got bigger. Larry deliberately left the nesting out for clarity, as he confirmed to me during our writing of the first camel together.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Didn't we have to wait for BASIC +2 for the nested modifiers? Time passes, memory fades:)

      Also, was LW's concern regarding parsing, that of being for difficult for the compiler, or the human being?


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      Hooray!
      Wanted!

        Didn't we have to wait for BASIC +2 for the nested modifiers? Time passes, memory fades:)

        Ordinary BASIC/PLUS allowed nested modifiers.

        Also, was LW's concern regarding parsing, that of being for difficult for the compiler, or the human being?

        The parsing would not be difficult for the computer. The concern is for the human being.

Re: One-liners: Conditionals in Loops
by BrowserUk (Pope) on Nov 04, 2003 at 21:43 UTC

    You can avoid having to add the newline by using the -l CLO which simplifies things. Which if your array isn't too big makes it

    perl -le"@a = <*.pl>; print for grep{ /\d{6}\./ } @a"

    If your array might be very large, then flattening it to a list doubles your memory consumption, and takes extra time. If that's a danger, then using

    perl -le" @a = <*.pl>; /\d{6}\./ and print for @a"

    avoids those problems.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!
    Wanted!

Re: One-liners: Conditionals in Loops
by krikbugs (Novice) on Nov 06, 2003 at 08:39 UTC
    Grep does this :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://304531]
Approved by gjb
Front-paged by Courage
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2014-08-27 12:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (237 votes), past polls