For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > December 2007 > Compressing a statement if X for Y to one line









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Compressing a statement if X for Y to one line
Yitzle

2007-12-09, 7:01 pm

Can the following code be done without any "code blocks"?
do {print "$_\n" if($_ > 3);} for (0..5);

This doesn't work:
print "$_\n" if($_ > 3) for (0..5);

In Python, you'd do something similar to:
print "$_\n" for (0..5) if($_ > 3);
but that doesn't work either.

If there a way to loop through a list and do a single statement on a
condition without using a {} block?

Thanks!
John W . Krahn

2007-12-09, 7:01 pm

On Sunday 09 December 2007 14:23, yitzle wrote:
>
> Can the following code be done without any "code blocks"?
> do {print "$_\n" if($_ > 3);} for (0..5);



$_ > 3 and print "$_\n" for 0 .. 5;


print map $_ > 3 ? "$_\n" : (), 0 .. 5;



John
--
use Perl;
program
fulfillment
Jeff Pang

2007-12-09, 10:01 pm

On Dec 10, 2007 6:23 AM, yitzle <yitzle@users.sourceforge.net> wrote:
> Can the following code be done without any "code blocks"?
> do {print "$_\n" if($_ > 3);} for (0..5);
>
> This doesn't work:
> print "$_\n" if($_ > 3) for (0..5);
>


$ perl -le 'print join" ",grep {$_>3} 0 ..5'
4 5
Yitzle

2007-12-09, 10:01 pm

Thanks for the replies!

On 12/9/07, John W. Krahn <krahnj@telus.net> wrote:
> $_ > 3 and print "$_\n" for 0 .. 5;


This one instantly appealed to me for some reason. I love it!
Reminds me of the Python version of the if operator (? :).
(I started reading Dive Into Python this last w. Strong typing? *shudder*)

> print map $_ > 3 ? "$_\n" : (), 0 .. 5;


I've got to learn to use map, but the if operator (or whatever its
named) if of limited use. It works for this specific case but does not
allow more complex operations. I'm not sure how much can be
accomplished with map.


On 12/9/07, Jeff Pang <jeffppl@gmail.com> wrote:
> $ perl -le 'print join" ",grep {$_>3} 0 ..5'


This solution is versatile [1], but the thought of looping through the
array twice makes me... unhappy. I know. Perl is a scripting language
and C will give better performance and in 99.99% cases the performance
difference will never be noticed, but I still feel that way. ;)

[1] perl -e 'print "$_\n" for (grep {$_ > 3} (0..5) );'
John W . Krahn

2007-12-09, 10:01 pm

On Sunday 09 December 2007 17:05, Jeff Pang wrote:
>
> On Dec 10, 2007 6:23 AM, yitzle <yitzle@users.sourceforge.net> wrote:
^^^^^^^^^^^^^^^^^^^^^^^^^
[color=darkred]
>
> $ perl -le 'print join" ",grep {$_>3} 0 ..5'

^^^^^^

Your example, without the code block, and with newlines in the correct
places:

perl -e'print map "$_\n", grep $_ > 3, 0 .. 5'



John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com