Re: use of ? operator instead of if
Jeff Westman wrote: At last! Thanks Bob, this /is/ how to use the conditional operator, while $VAL ? ( $VAL = $VAL:$expr ) : ( $VAL = $expr ) /isn't/. ?: is an /operator/. It happens to have three operands instead of the usual two or 1, but it is meant for deriving a new value from three others, like a function. sub conditional { my ($test, $val1, $val2); return $val1 if $test; return $val2; } $VAL = conditional ($VAL, $VAL:$expr, $expr) Rob If you ask /me/ the above code is a lot harder to read to the simple one-line ternary as was mentioned before (expr:true?false). Hi Jeff, I certainly agree, and I'm sure Rob would too. I don't hink his point was to recommend the conditional() function for actual use, but to outline in function form the logic implemented by the operator. The ternary operation is used to make your code more concise and readable, but in a limited context. IE, you can nest ternary operations ... have fun maintaining it! -JW Absolutely. Terse operations can certainly add immediacy and punch to code when the context is small enough to be crystal clear. Trying to pack complicated logic into them simply imposes an equal or greater burden of unpacking the logic whe reviewing the code. Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: use of ? operator instead of if
Scott R. Godin wrote: Jeff Westman wrote: It's called the ternary operator, and works like this: $VAL ? $VAL = $VAL:$expr : $VAL = $expr; couldn't this be written as $VAL = $VAL ? $VAL:$expr : $expr; Exactly Scott. See my post in this thread. Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
use of ? operator instead of if
Hi: I have this statement which checks for existence of the $VAL variable and performs certain actions if($VAL){ $VAL = $VAL:$expr; } else { $VAL = $expr; } Can this 4 line statement be reduced to a single line using ? operator? Thanks Ravi __ Do you Yahoo!? Yahoo! Tax Center - File online, calculators, forms, and more http://platinum.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: use of ? operator instead of if
It's called the ternary operator, and works like this: $VAL ? $VAL = $VAL:$expr : $VAL = $expr; IF_EXPR ? THEN_EXPR : ELSE_EXPR Depending on context of how you are using it, I like to put the enter statement in parens as in: ( ternary_statement ) -Jeff --- Ravi Malghan [EMAIL PROTECTED] wrote: Hi: I have this statement which checks for existence of the $VAL variable and performs certain actions if($VAL){ $VAL = $VAL:$expr; } else { $VAL = $expr; } Can this 4 line statement be reduced to a single line using ? operator? Thanks Ravi __ Do you Yahoo!? Yahoo! Tax Center - File online, calculators, forms, and more http://platinum.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] __ Do you Yahoo!? Yahoo! Tax Center - File online, calculators, forms, and more http://platinum.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: use of ? operator instead of if
Ravi Malghan wrote: Hi: I have this statement which checks for existence of the $VAL variable and performs certain actions if($VAL){ $VAL = $VAL:$expr; } else { $VAL = $expr; } Can this 4 line statement be reduced to a single line using ? operator? $VAL = $VAL ? $VAL:$expr : $expr; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: use of ? operator instead of if
Sure, something like the following: $a = ( $b 1 ) ? b greater than 1 : b not greater than 1; --jess -Original Message- From: Ravi Malghan [mailto:[EMAIL PROTECTED] Sent: Tuesday, April 01, 2003 12:45 PM To: [EMAIL PROTECTED] Subject: use of ? operator instead of if Hi: I have this statement which checks for existence of the $VAL variable and performs certain actions if($VAL){ $VAL = $VAL:$expr; } else { $VAL = $expr; } Can this 4 line statement be reduced to a single line using ? operator? Thanks Ravi __ Do you Yahoo!? Yahoo! Tax Center - File online, calculators, forms, and more http://platinum.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: use of ? operator instead of if
Bob Showalter wrote: Ravi Malghan wrote: Hi: I have this statement which checks for existence of the $VAL variable and performs certain actions if($VAL){ $VAL = $VAL:$expr; } else { $VAL = $expr; } Can this 4 line statement be reduced to a single line using ? operator? $VAL = $VAL ? $VAL:$expr : $expr; At last! Thanks Bob, this /is/ how to use the conditional operator, while $VAL ? ( $VAL = $VAL:$expr ) : ( $VAL = $expr ) /isn't/. ?: is an /operator/. It happens to have three operands instead of the usual two or 1, but it is meant for deriving a new value from three others, like a function. sub conditional { my ($test, $val1, $val2); return $val1 if $test; return $val2; } $VAL = conditional ($VAL, $VAL:$expr, $expr) Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: use of ? operator instead of if
--- Rob Dixon [EMAIL PROTECTED] wrote: Bob Showalter wrote: Ravi Malghan wrote: Hi: I have this statement which checks for existence of the $VAL variable and performs certain actions if($VAL){ $VAL = $VAL:$expr; } else { $VAL = $expr; } Can this 4 line statement be reduced to a single line using ? operator? $VAL = $VAL ? $VAL:$expr : $expr; At last! Thanks Bob, this /is/ how to use the conditional operator, while $VAL ? ( $VAL = $VAL:$expr ) : ( $VAL = $expr ) /isn't/. ?: is an /operator/. It happens to have three operands instead of the usual two or 1, but it is meant for deriving a new value from three others, like a function. sub conditional { my ($test, $val1, $val2); return $val1 if $test; return $val2; } $VAL = conditional ($VAL, $VAL:$expr, $expr) Rob If you ask /me/ the above code is a lot harder to read to the simple one-line ternary as was mentioned before (expr:true?false). The ternary operation is used to make your code more concise and readable, but in a limited context. IE, you can nest ternary operations ... have fun maintaining it! -JW __ Do you Yahoo!? Yahoo! Tax Center - File online, calculators, forms, and more http://platinum.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]