in reply to Ternary if versus normal if question
You can see that the 2nd assignment operator has lower precedence than the ternary operator. So this says,(($lights{$light} ? ($lights{$light} = 0) : $lights{$light}) = 1);
If $lights{$light} is true, then assign 1 to the result of "$lights{$light}=0", otherwise assign 1 to the result of $lights{$light}.... which is not what you want. A better way to use the ternary here is:
Or even better still is probably:$lights{$light} = $lights{$light} ? 0 : 1;
which toggles $lights{$light} between true & false.$lights{$light} = ! $lights{$light};
blokhead
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Ternary if versus normal if question
by xdg (Monsignor) on Nov 17, 2005 at 14:08 UTC |