Re: Boolean counter? by Corion (Pope) on Dec 07, 2009 at 08:06 UTC |
$counter = $counter xor 1
or
$counter ^= 1
(tested with perl -wle "print $counter ^=1 for 1..3") | [reply] [d/l] [select] |
|
I agree that the XOR solution is perfectly correct, but why use that instead of NOT? Isn't
$counter = ! $counter;
easier to read? That said, I have to admit that I like the snazzyness of the in place ^= syntax. It is hard to get more concise than that.
- doug
| [reply] [d/l] |
|
The difference is that $toggle ^= 1; leaves you with a number (0 or 1) while $toggle = !$toggle; leaves you with a boolean.
Honestly, it sounds like the OP wants a boolean, so the easier to read negation should be used. If he wants to display the boolean, that's an outputting formatting issue to be resolved then.
| [reply] [d/l] [select] |
Re: Boolean counter? by Utilitarian (Priest) on Dec 07, 2009 at 08:06 UTC |
| [reply] [d/l] [select] |
Re: Boolean counter? by ikegami (Pope) on Dec 07, 2009 at 08:08 UTC |
if ($toggle) {
$toggle = 0;
} else {
$toggle = 1;
}
can be written as
$toggle = $toggle ? 0 : 1;
And if you wanted a bitwise toggle, you could do
$toggle ^= 1;
But you asked for boolean, so you want the even simpler:
$toggle = !$toggle;
| [reply] [d/l] [select] |
Re: Boolean counter? by eye (Hermit) on Dec 07, 2009 at 08:18 UTC |
$toggle = 1 - $toggle;
This lacks elegance since it needs to start at 0 or 1, but it is simple. | [reply] [d/l] |
Re: Boolean counter? by moritz (Cardinal) on Dec 07, 2009 at 08:21 UTC |
You can just use ++ to increment your counter, but when you ask for its value use $counter % 2 instead of the counter itself.
Or if you're not interested in the numeric value 0, but rather that it's a false value, you can also use this update step:
$counter = !$counter;
| [reply] [d/l] [select] |
|
Or if you do want the numerical value:
$counter = !$counter || 0;
| [reply] [d/l] |
Re: Boolean counter? by GrandFather (Cardinal) on Dec 07, 2009 at 08:58 UTC |
print $|-- for 1 .. 4;
Prints:
1010
Although I wouldn't recommend it in a context where you wish to win friends and influence people among the bossing classes.
True laziness is hard work
| [reply] [d/l] [select] |
Re: Boolean counter? by JavaFan (Canon) on Dec 07, 2009 at 12:00 UTC |
Just to prove you can use trigs to solve almost anything:
$counter = int cos $counter * atan2(1, 0);
| [reply] [d/l] |
Re: Boolean counter? by Ratazong (Prior) on Dec 07, 2009 at 12:19 UTC |
Just to show that you can make a lot with "lookup-tables" ;-)
$counter = (1, 0)[$counter];
Rata | [reply] [d/l] |
|
$counter = [ 1 => 0 ] -> [ 1 <= $counter ];
| [reply] [d/l] |
|
ug, you create two variables every time you toggle it (one of which is an array!), and you rely on the undocumented values of <= returns. And the latter doesn't help make it prettier at all!
| [reply] [d/l] |
Re: Boolean counter? by vitoco (Pilgrim) on Dec 07, 2009 at 12:47 UTC |
Two more approaches:
- Using hashes:
my %Next = ( 0 => 1, 1 => 0);
my $Counter = 0;
#...
$Counter = $Next{$Counter};
- Decreasing instead of increasing:
$Counter = abs --$Counter;
| [reply] [d/l] [select] |
|
| [reply] [d/l] |
|
The behaviour of that is actually not defined. You may end up with $Counter being -1, or with purple daemons coming out of your USB port, ready to chew off your fingers.
| [reply] |
|
|
|
|
|
|
$toggle = - --$toggle;
should be
$toggle = -( $toggle - 1 );
which can be shortened to previously mentioned
$toggle = 1 - $toggle;
| [reply] [d/l] [select] |
Re: Boolean counter? by rowdog (Curate) on Dec 07, 2009 at 22:11 UTC |
I tend to use
$toggle *= -1;
Update: I stand corrected, thanks LanX. I often use that for alternating rows but it's wrong here.
| [reply] [d/l] |
|
well it's a toggle but can't be used as boolean as the OP wanted , since 0*-1=0
| [reply] [d/l] |