Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > ? : statement not working as expected

Reply
Thread Tools

? : statement not working as expected

 
 
Patrick Hartman
Guest
Posts: n/a
 
      03-09-2010
Hi, I am trying to use ? : statements instead of if/else blocks
wherever possible to keep my code shorter. Below is a conditional
statement that for some reason is not working as expected with the ? :
method:

#!/usr/bin/perl -w
use strict;

# image name
my $image = 'yf446_mk_nb_0_0_6_ia';

# break out seperate pieces
my($bare_style,$color,$brand,$slot1,$slot2,$shot,$ type) = $image =~ m/
^(\w+)_(\w+)_(\w+)_(\w+)_(\w+)_(\w+)_(\w+)$/;


# this is not working as expected, returns the 'else' result
{
my $style;
$brand eq 'nb' ? $style = $bare_style . $color : $style =
$bare_style;
print "?: version: $style\n";
}

# this works as expected
{
my $style;

if ($brand eq 'nb') {
$style = $bare_style . $color;
}
else {
$style = $bare_style;
}

print "if/else block version: $style\n";
}


Any ideas why this would not work in the first statement? I appreciate
it,

Patrick
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      03-09-2010
>>>>> "PH" == Patrick Hartman <> writes:

PH> Hi, I am trying to use ? : statements instead of if/else blocks
PH> wherever possible to keep my code shorter. Below is a conditional
PH> statement that for some reason is not working as expected with the ? :
PH> method:

this is a classic newbie misuse of ?:.

PH> my $style;
PH> $brand eq 'nb' ? $style = $bare_style . $color : $style =
PH> $bare_style;
PH> print "?: version: $style\n";

$style = $brand eq 'nb' ? "$bare_style$color" : $bare_style ;

?: is meant to return an expression based on a boolean. it it NOT meant
to do side effects like assignment. you could fix your code with () but
it would still be wrong. the issue was precedence. the proper solution i
pasted above. the whole idea is to assign once but choose what to
assign. then the precedence (= is higher binding than ? works to your
advantage.

and given what you are doing, there are other ways too:

$style = $bare_style . ($brand eq 'nb' ? $color : '') ;

$style = $bare_style ;
$style .= $color if $brand eq 'nb' ;

uri

--
Uri Guttman ------ -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
 
 
 
Ted Zlatanov
Guest
Posts: n/a
 
      03-09-2010
On Tue, 9 Mar 2010 09:33:44 -0800 (PST) Patrick Hartman <> wrote:

PH> # image name
PH> my $image = 'yf446_mk_nb_0_0_6_ia';

PH> # break out seperate pieces
PH> my($bare_style,$color,$brand,$slot1,$slot2,$shot,$ type) = $image =~ m/
PH> ^(\w+)_(\w+)_(\w+)_(\w+)_(\w+)_(\w+)_(\w+)$/;

Doesn't it bother you that there's so much repetition there?

split('_', $image) will return a list of elements just as easily.

Also note that \w will match the '_' character as well so your
expression can only work accidentally.

PH> # this is not working as expected, returns the 'else' result
PH> {
PH> my $style;
PH> $brand eq 'nb' ? $style = $bare_style . $color : $style =
PH> $bare_style;
PH> print "?: version: $style\n";
PH> }

Never, ever assign in a conditional. Those who break that rule usually
do it for a very good reason (e.g. in an input loop) but 99% of the time
you simply should not do it.

PH> # this works as expected
PH> {
PH> my $style;

PH> if ($brand eq 'nb') {
PH> $style = $bare_style . $color;
PH> }
PH> else {
PH> $style = $bare_style;
PH> }

PH> print "if/else block version: $style\n";
PH> }

PH> Any ideas why this would not work in the first statement? I appreciate
PH> it,

Uri explained, but really, which one would you rather see in a program?

Ted
 
Reply With Quote
 
Patrick Hartman
Guest
Posts: n/a
 
      03-09-2010
Thanks for the explanation and insight Uri and Ted, I appreciate it. I
am clearly using the conditional too often (yes I am a newbie). Also
Ted thanks for pointing out the problem with my expression, I forgot
that \w includes stuff besides letters.

Patrick
 
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
End of statement expected PaulT ASP .Net 0 10-02-2008 10:23 PM
End of statement expected error momo ASP .Net 2 03-20-2006 11:34 PM
BC30205: End of statement expected. JJY ASP .Net 1 12-23-2003 02:25 AM
Expected end of statement problem Graham James Campbell CS2000 ASP General 7 10-03-2003 08:51 PM
execute + ArraySearch causes "Expected end of statement" error - Why? Phil Powell ASP General 1 07-10-2003 03:06 AM



Advertisments