Q: conditional operator and variable assignment 
Author Message
 Q: conditional operator and variable assignment

   Why can't I assign to a variable when using a conditional operator?
I.e, the syntax "if-test ? if-true : if-false" doesn't permit me to
perform a scalar assignment if the test returns true.  Example:

   #!/usr/bin/perl
   $a = $b = "";
   5 == 2 + 3 ? print "true\n"  : print "false\n";
   5 == 2 + 3 ? $a = "true"     : $a = "false";
   print "the value of \$a is $a\n";

   if ( 5 == 2 + 3) { $b = "true" } else { $b = "false" };
   print "the value of \$b is $b\n";
   #---end-of-script---

This script produces the following output:

   true
   the value of $a is false
   the value of $b is true

   Why isn't the value of $a set to "true"? What do I have to do to
alter the conditional test to fix it (or is it unfixable)?

   I would also like to do a substitution that looks something like this
-- greatly simplified for readability:

       s/^(.)bc/($1 eq "a" ? "A" : "z")ef/ge;
       #
       # if given "abc", print "Aef"
       # if given "bbc", print "zef", and so on for any regex "[^a]bc"

Even if the preceding question about the s/ubsti/tution/ is answered, I
would still like to know why the conditional test failed to assign the
variables as I expected. Thanks in advance!

--

senior editor, Cornerstone magazine
http://www.*-*-*.com/
939 W. Wilson Ave., Chicago, IL 60640-5706
tel: 773/561-2450, 1-(ext.)2084   fax: 773/989-2076



Sun, 08 Jul 2001 03:00:00 GMT  
 Q: conditional operator and variable assignment

Quote:

>    5 == 2 + 3 ? $a = "true"     : $a = "false";

What you want to do is this:

   $a = (5 == 2 + 3) ? 'true' : 'false';

You might want to look at the examples in perlop again.

HTH,
Andre



Sun, 08 Jul 2001 03:00:00 GMT  
 Q: conditional operator and variable assignment

Quote:

>    I would also like to do a substitution that looks something like this
> -- greatly simplified for readability:

>        s/^(.)bc/($1 eq "a" ? "A" : "z")ef/ge;
>        #
>        # if given "abc", print "Aef"
>        # if given "bbc", print "zef", and so on for any regex "[^a]bc"

You were close. You just needed to concatenate "ef" with the result of the
preceding operation (you're in /e mode, remember). Using -w would have
told you what was wrong.

   s/^(.)bc/($1 eq 'a' ? 'A' : 'z').'ef'/ge;
                                   ^
HTH,
Andre



Sun, 08 Jul 2001 03:00:00 GMT  
 Q: conditional operator and variable assignment
[Posted and a courtesy copy mailed.]



Quote:
>    Why can't I assign to a variable when using a conditional operator?
> I.e, the syntax "if-test ? if-true : if-false" doesn't permit me to
> perform a scalar assignment if the test returns true.  Example:
...
>    5 == 2 + 3 ? $a = "true" : $a = "false";

...

Quote:
>    Why isn't the value of $a set to "true"? What do I have to do to
> alter the conditional test to fix it (or is it unfixable)?

You have to correct the precedences!  The above statement is parsed as:

     (5 == 2 + 3 ? ($a = "true") : $a) = "false";

So "false" is assigned to $a.  You need to parenthesize the second
assignment:

     5 == 2 + 3 ? $a = "true" : ($a = "false");

The reason you don't have to parenthesize the first assignment also is
that the parser knows to gobble up and evaluate all of the expression
between the '?' and the ':'.  I believe this to be a foolish innovation
introduced in C++, and prefer to be explicit about both bindings,
because of their apparent (to me) symmetry:

     5 == 2 + 3 ? ($a = "true") : ($a = "false");

Of course, if this is all you wanted, you could simpler write:

     $a = 5 == 2 + 3 ? "true" : "false";

but you are after deeper things, I know.

Quote:
>    I would also like to do a substitution that looks something like this
> -- greatly simplified for readability:

>        s/^(.)bc/($1 eq "a" ? "A" : "z")ef/ge;
>        #
>        # if given "abc", print "Aef"
>        # if given "bbc", print "zef", and so on for any regex "[^a]bc"

The expression you evaluate must be valid Perl, not a combination of an
expression stuck next to a bareword.  It is not a string.

         s/^(.)bc/($1 eq "a" ? "A" : "z") . 'ef'/ge;

By the way, in all this you use double-quotes when there is no
interpolation.  Better style, IMO, would be to use single-quotes, which
adds 'visual semantics' to your code.

--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/



Sun, 08 Jul 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. A question on the Conditional Operator

2. Conditional Operator behavior?

3. Variable assignments from variable values

4. Three Operand Conditional Operator

5. Ternary Conditional Operator Question

6. push onto trinary conditional operator?

7. newbie questions re conditional operators

8. ternary conditional operator

9. conditional operator "?:"

10. problem parsing conditional operator.

11. Assignment operators - syntactic sugar

12. Overloaded assignment operator bug?

 

 
Powered by phpBB® Forum Software