Re: Boolean counter?
by Corion (Patriarch) 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 ikegami (Patriarch) 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 Utilitarian (Vicar) on Dec 07, 2009 at 08:06 UTC
|
| [reply] [d/l] [select] |
Re: Boolean counter?
by GrandFather (Saint) 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 Ratazong (Monsignor) 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 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 eye (Chaplain) 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 vitoco (Hermit) 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] |
|
$toggle = - --$toggle;
should be
$toggle = -( $toggle - 1 );
which can be shortened to previously mentioned
$toggle = 1 - $toggle;
| [reply] [d/l] [select] |
|
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] |
|
|
|
|
|
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 hdb (Monsignor) on Mar 04, 2015 at 13:07 UTC
|
I cannot resist. You can also use a closure, every time you call it you get 0 or 1 alternatingly. You can create as many independent toggles as you want.
use strict;
use warnings;
sub create_toggle {
my $flag = 0;
return sub { $flag = 1-$flag } # your favorite method here
}
my $toggle = create_toggle;
print $toggle->()."\n" for 1..10;
UPDATE: ...or like this
use strict;
use warnings;
{
package Toggler;
sub new {
my $flag = 0;
return sub { $flag = 1-$flag }
}
}
my $toggle = new Toggler;
print $toggle->()."\n" for 1..10;
| [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] |
|
$toggle = -1 * ($toggle - 0.5) + 0.5;
Rata (loving this thread) | [reply] [d/l] |