Compressing a statement if X for Y to one line

Compressing a statement if X for Y to one line

am 09.12.2007 23:23:03 von yitzle

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!

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Compressing a statement if X for Y to one line

am 09.12.2007 23:43:42 von krahnj

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

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Compressing a statement if X for Y to one line

am 10.12.2007 02:05:42 von Jeff Pang

On Dec 10, 2007 6:23 AM, yitzle 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

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Compressing a statement if X for Y to one line

am 10.12.2007 02:49:39 von yitzle

Thanks for the replies!

On 12/9/07, John W. Krahn 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 week. 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 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) );'

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Compressing a statement if X for Y to one line

am 10.12.2007 03:09:22 von krahnj

On Sunday 09 December 2007 17:05, Jeff Pang wrote:
>
> On Dec 10, 2007 6:23 AM, yitzle 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'
^^^^^^

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

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/