--- 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]