[Soap box warning...]

You seem to be suffering from a common perl programming psychosis: the sometimes unbearable urge to write clever perl in the fewest possible number of lines ... that infects us all from time to time...

Read Damian Conway's Perl Best Practices.  And when you feel the need to write clever code, read it again.

Take a step back and think about the difference between the following... (besides the fact that the first way doesn't work)

if ($_ != 3) for (@a) {
}  


for (@a) {
        if ($_ != 3) {
        }
}

Two brackets?   The for-if version is readily understandable to anyone who reads your code (including yourself 6 - 9 - 12 months later).  Your if-for version, even if it did work, will confuse many, most, all, maybe even yourself 6 months later.

Perl Best Practices:  Chapter 1: Page 4:  "... optimize your programming style for readibility; not writability.  Better yet, try to optimize for comprehensibility; easy-to-read and easy-to-understand are not necessarily the same thing."

And Perl Best Practices would suggest that the following is stylistically best from a readibility / comprehensibility perspective as it avoids the use of the magic variable $_

foreach my $a (@a) {
        if ($a != 3) {
                print "$a\tsomething\n";
        }
}

Save cleverness for the times when it could make a signficant, measurable difference.  Saving two brackets is useless cleverness.

Concerning your if-for code that doesn't work, see "perlsyn" in the ActivePerl user guide.

"Any simple statement may optionally be followed by a SINGLE modifier, just before the terminating semicolon (or block ending). The possible modifiers are:

    if EXPR
    unless EXPR
    while EXPR
    until EXPR
    foreach LIST"

So this is legal syntax:

print "$_\tsomething\n" foreach (@a);

But this is not because the if block is not a simple statement.

if ($_ != 3) {
        print "$_\tsomething\n";
} foreach (@a);


Maybe the closest to your envisioned clever solution is the "apply" function available in the List::MoreUtils perl module.  See the last example in the following that uses "apply".

use strict;
use warnings;

my @a = (1,2,3,4,5);

print "$_\tsomething 1\n" foreach (@a);

# this isn't legal perl syntax...
#
#if ($_ != 3) {          
#        print "$_\tsomething 1\n";
#} foreach (@a);        
       
print "\nstandard foreach loop with interior if\n";
foreach my $a (@a) {
        if ($a != 3) {
                print "$a\tsomething 2\n";
        }
}


print "\nstandard foreach loop with next\n";
foreach my $a (@a) {
        next if ($a == 3);
        print "$a\tsomething 3\n";
}

print "\nList::MoreUtils apply method\n";

use List::MoreUtils qw(apply);

apply { print "$_\tsomething 4\n" if ($_ != 3)        } @a;




Regards,

... Dewey




"Ng, Bill" <[EMAIL PROTECTED]>
Sent by: [EMAIL PROTECTED]

04/18/2006 03:59 PM

To
<[email protected]>
cc
Subject
Iffor





Syntax issue (I think),

I'm trying to do the following:
I need to execute a block of instructions for all items in an array
except for one.

So if my array was:
@a=(1,2,3,4,5);
And we assume that I don't want to execute the block if the value of $_
is 3 ...

Then, in my head, I'm looking for the WORKING (key word there) version
of this:
-----------
@a = (1,2,3,4,5);
if ($_ != 3) for (@a)
{
 print "something";
 &doSomething();
 print "somethingelse";
 &yada($yada{$ya})
}
-------------

But that if ... for line doesn't work.
Neither does:
------------------------
for (@a, $_ != 3) {}
------------------------

Anyone got any ideas?  I've always done this by running a standard for
loop and having an if condition inside the loop .. but that seems
inefficient to me right now and I'm looking for the easier way.

Bill Ng

_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to