Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > more than one statement in a post perlish condition

Reply
Thread Tools

more than one statement in a post perlish condition

 
 
Alexander Jack
Guest
Posts: n/a
 
      08-11-2009
Dear community ,
I wanted to know that how do I give more than one statement in a post
perlish condition ,

for ex:
print "YES" if ( $yes eq "yes");


I want to change the value of $yes if it is true that means it should
execute after
print statement.

Thanks in Advance
 
Reply With Quote
 
 
 
 
Peter Makholm
Guest
Posts: n/a
 
      08-11-2009
Alexander Jack <(E-Mail Removed)> writes:

> Dear community ,
> I wanted to know that how do I give more than one statement in a post
> perlish condition ,
>
> for ex:
> print "YES" if ( $yes eq "yes");


For more than one simple statement I would almost always use a real if
block

if ($yes eq "yes") {
print "YES";
$yes = "no;
}

If you for some reason want to hide the condition as a statement
modifier you could use a do block:

do {
print "YES";
$yes = "no";
} if $yes eq "yes";

It is possible to do something like

print("YES"), $yes = "no" if $yes eq "yes";
(print "YES"), $yes = "no" if $yes eq "yes";
print "YES" and $yes = "no" if $yse eq "yes";

but I consider all of them less readable than the block forms and at
least the last example isn't obvious for people not quite used to perl
precedence rules.

//Makholm.
 
Reply With Quote
 
 
 
 
Peter Makholm
Guest
Posts: n/a
 
      08-11-2009
Ben Morrow <(E-Mail Removed)> writes:

>> print("YES"), $yes = "no" if $yes eq "yes";
>> (print "YES"), $yes = "no" if $yes eq "yes";
>> print "YES" and $yes = "no" if $yse eq "yes";

>
> The last example is also Wrong, in that print can fail. It's not common,


Correct, I should have mentioned that the last one wasn't semantically
equivalent with the others.

//Makholm
 
Reply With Quote
 
Justin C
Guest
Posts: n/a
 
      08-13-2009
On 2009-08-11, Alexander Jack <(E-Mail Removed)> wrote:
> Dear community ,
> I wanted to know that how do I give more than one statement in a post
> perlish condition ,
>
> for ex:
> print "YES" if ( $yes eq "yes");
>
> I want to change the value of $yes if it is true that means it should
> execute after
> print statement.


print "yes" if (($yes eq "yes") && ($yes = 'no'));


Justin.

--
Justin C, by the sea.
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      08-13-2009
>>>>> "JC" == Justin C <(E-Mail Removed)> writes:

JC> On 2009-08-11, Alexander Jack <(E-Mail Removed)> wrote:
>> Dear community ,
>> I wanted to know that how do I give more than one statement in a post
>> perlish condition ,
>>
>> for ex:
>> print "YES" if ( $yes eq "yes");
>>
>> I want to change the value of $yes if it is true that means it should
>> execute after
>> print statement.


JC> print "yes" if (($yes eq "yes") && ($yes = 'no'));

it is nice to be helpful but better to be correct. the latter is = which
is very wrong in two ways: it is an assignment and not a comparison and
it is also numeric (assuming you meant ==) and not a string
comparison. then the logic is also tortured. why would you check for
'yes' and ALSO 'no'. it can't be both. next the OP wanted multiple
statements with a single modifier, not multiple booleans in one
modifier. finally all the parens aren't needed and are noisy.

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      08-13-2009
Uri Guttman wrote:
)>>>>> "JC" == Justin C <(E-Mail Removed)> writes:
)
) JC> On 2009-08-11, Alexander Jack <(E-Mail Removed)> wrote:
) >> I want to change the value of $yes if it is true that means it should
) >> execute after
) >> print statement.
)
) JC> print "yes" if (($yes eq "yes") && ($yes = 'no'));
)
) it is nice to be helpful but better to be correct. the latter is = which
) is very wrong in two ways: it is an assignment and not a comparison and
) it is also numeric (assuming you meant ==) and not a string
) comparison. then the logic is also tortured. why would you check for
) 'yes' and ALSO 'no'. it can't be both. next the OP wanted multiple
) statements with a single modifier, not multiple booleans in one
) modifier. finally all the parens aren't needed and are noisy.

*whoosh*


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
Scott Bryce
Guest
Posts: n/a
 
      08-13-2009
Uri Guttman wrote:
>
> JC> print "yes" if (($yes eq "yes") && ($yes = 'no'));
>
> it is nice to be helpful but better to be correct. the latter is =
> which is very wrong in two ways: it is an assignment and not a
> comparison and it is also numeric (assuming you meant ==) and not a
> string comparison. then the logic is also tortured. why would you
> check for 'yes' and ALSO 'no'. it can't be both. next the OP wanted
> multiple statements with a single modifier, not multiple booleans in
> one modifier. finally all the parens aren't needed and are noisy.



Did I miss something? What the OP wants to do is evaluate $yes. If $yes
contains 'yes', then print it and change its value.

The line if code JC provided does that. If $yes evaluates to 'yes' then
$yes is set to 'no'. Since setting $yes to 'no' evaluates to TRUE, the
print statement is executed.




use strict;
use warnings;

my $yes = 'yes';

print 'yes' if (($yes eq 'yes') && ($yes = 'no'));

print "\nYes now equals $yes";


----------

yes
Yes now equals no
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      08-13-2009
>>>>> "SB" == Scott Bryce <(E-Mail Removed)> writes:

SB> Uri Guttman wrote:
>>

JC> print "yes" if (($yes eq "yes") && ($yes = 'no'));
>>
>> it is nice to be helpful but better to be correct. the latter is =
>> which is very wrong in two ways: it is an assignment and not a
>> comparison and it is also numeric (assuming you meant ==) and not a
>> string comparison. then the logic is also tortured. why would you
>> check for 'yes' and ALSO 'no'. it can't be both. next the OP wanted
>> multiple statements with a single modifier, not multiple booleans in
>> one modifier. finally all the parens aren't needed and are noisy.



SB> Did I miss something? What the OP wants to do is evaluate $yes. If $yes
SB> contains 'yes', then print it and change its value.

SB> The line if code JC provided does that. If $yes evaluates to 'yes' then
SB> $yes is set to 'no'. Since setting $yes to 'no' evaluates to TRUE, the
SB> print statement is executed.

not what i read but i could be wrong. in any case (and regardless of the
correctness of the above code) it is horrible. using assignment INSIDE a
conditional of a modifier is nuts. it should never be done especially in
a compound boolean. it LOOKS like a bug and will always be read as one.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
Nathan Keel
Guest
Posts: n/a
 
      08-13-2009
Willem wrote:

> Uri Guttman wrote:
> )>>>>> "JC" == Justin C <(E-Mail Removed)> writes:
> )
> ) JC> On 2009-08-11, Alexander Jack <(E-Mail Removed)> wrote:
> ) >> I want to change the value of $yes if it is true that means it
> should
> ) >> execute after
> ) >> print statement.
> )
> ) JC> print "yes" if (($yes eq "yes") && ($yes = 'no'));
> )
> ) it is nice to be helpful but better to be correct. the latter is =
> which ) is very wrong in two ways: it is an assignment and not a
> comparison and ) it is also numeric (assuming you meant ==) and not a
> string ) comparison. then the logic is also tortured. why would you
> check for ) 'yes' and ALSO 'no'. it can't be both. next the OP wanted
> multiple ) statements with a single modifier, not multiple booleans in
> one ) modifier. finally all the parens aren't needed and are noisy.
>
> *whoosh*
>
>
> SaSW, Willem


LOL!
 
Reply With Quote
 
Peter Makholm
Guest
Posts: n/a
 
      08-13-2009
Justin C <(E-Mail Removed)> writes:

>> print "YES" if ( $yes eq "yes");
>>
>> I want to change the value of $yes if it is true that means it should
>> execute after
>> print statement.

>
> print "yes" if (($yes eq "yes") && ($yes = 'no'));


As Uri has explained this is very prone to mis-readings and is thus
unmaintainable. If we go by the example you solution works, but the
assignment happens *before* the print statement, which could be argued
to be wrong according to "the spec".

//Makholm





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can one declare more than one signal on one line? Merciadri Luca VHDL 4 11-01-2010 02:00 PM
can I use more than one status condition name in imaplib.status() harryos Python 0 10-13-2010 01:37 PM
writing if statement in one line with elsif condition Luiz Vitor Martinez Cardoso Ruby 9 08-17-2008 10:31 PM
One question about FLOAT number as condition statement G C++ 3 12-21-2006 11:15 AM
Condition outside loop or separate loop for different condition? - Java 12 06-15-2005 08:50 AM



Advertisments