in reply to nested tabular ternary
If I understand what you're asking for, you want:# this gives $result as 1, not 33. Makes sense, # since $val == 4 evaluates to 1. But, I want $result to # be assigned 33, based on the fact that $val == 4. $result = ($major_length == 4800) ? 18 : ($major_length == 8552) ? ($val == 4) : 33;
You can do this with nested ternaries (though it's a little messy), but if you only want to use ternaries, you'll have to specify what to set $result to if none of the conditions is met (eg. $result = undef):if ($major_length == 4800) { $result = 18; } elsif ($major_length == 8552) { if ($val == 4) { $result = 33; } else { # $result is Unspecified (see A below) } } else { # $result is Unspecified (see B below) }
But it's messy as I said, and unless you really want to set $result to some value, it's probably better to use conditionals (eg. if...else), both for your clarity and the clarity of those who may read your code in the future ;-)$result = ($major_length == 4800)? 18: (($major_length == 8552)? (($val == 4)? 33: undef): # see A above undef); # see B above
|
|---|