On 9/11/06, John W. Krahn <[EMAIL PROTECTED]> wrote:
Jen Spinney wrote:
> Hello. It's obvious that I need to factor the follow code, but I'm
> not sure how to go about it syntax wise (I read perldoc perlsyn, but
> I'm still not sure of the best way).
>
> if ($color eq $B_COLOR)
> {
> $id = $c->createLine
> ($x[0], $time1,
> $x[1], $time1,
> -arrow => 'last';
> -fill => $color,
> -activefill => 'black',
> -width => 2.0,
> -tags => [$direction, $color]
> );
> }
> else
> {
> $id = $c->createLine
> ($x[0], $time1,
> $x[1], $time1,
> $x[2], $time2,
> $x[3], $time2,
> -arrow => 'last',
> -fill => $color,
> -activefill => 'black',
> -width => 2.0,
> -tags => [$direction, $color],
> );
> }
>
> The only difference between the blocks is two additional lines
> ($x[2]...) in the else block. I mean, I know I could write a
> subroutine that I could call inside the parentheses that would return
> a string (either empty or containing "$x[2]... depending on the result
> of the condition test), but that seems clunky. There seems like there
> should be a simpler way. Any thoughts? Thanks for any help! - Jen
It looks like you may want something like this:
$id = $c->createLine(
$x[0], $time1,
$x[1], $time1,
$color eq $B_COLOR ? () : (
$x[2], $time2,
$x[3], $time2,
),
-arrow => 'last',
-fill => $color,
-activefill => 'black',
-width => 2.0,
-tags => [$direction, $color],
);
John
John -
Ah yes. Thank you very much. I didn't realize the ? : operators were
so powerful -- I just thought they were used for one line assignments.
Have a good day/night/whatever!
- Jen
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>